"""
***************************************************************************
    LicenseMake.py
    ---------------------
    Date                 : November 2012
    Copyright            : (C) 2012 by Victor Olaya
    Email                : volayaf at gmail dot com
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = 'wanger'
__date__ = 'November 2024'
__copyright__ = '(C) 2012, siwei'

import os
from cryptography.fernet import Fernet
from PyQt5.QtGui import QIcon

from qgis._core import QgsCoordinateReferenceSystem, QgsProcessingParameterCrs, QgsProcessingParameterFileDestination
from qgis.core import (QgsProcessing,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterDefinition,
                       QgsProcessingParameterString,
                       QgsProcessingParameterDateTime,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterVectorDestination)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]


class LicenseMake(GdalAlgorithm):
    HOST = 'LICENSEHOST'
    Date = 'LICENSEDATE'
    OUTPUT = 'OUTPUT'

    def __init__(self):
        super().__init__()

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterString(self.HOST,
                                                       self.tr('服务器机器名')))
        self.addParameter(
            QgsProcessingParameterDateTime(self.Date, '许可到期时间', type=QgsProcessingParameterDateTime.Type.Date))

        # 创建一个输出参数，指定为文本文件类型
        output_parameter = QgsProcessingParameterFileDestination(
            self.OUTPUT,
            self.tr('导出位置'),
            '.txt', defaultValue='license.txt')
        self.addParameter(output_parameter)

    def name(self):
        return 'licensemake'

    def displayName(self):
        return self.tr('生成许可文件')

    def group(self):
        return self.tr('软件许可')

    def groupId(self):
        return 'licensetools'

    def icon(self):
        return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'certification.png'))

    def commandName(self):
        return 'ogr2ogr'

    def generateLicense(self, parameters):
        print("咱这块正在生成许可文件呢，别着急")
        outputpath = parameters.get("OUTPUT")
        custom_format = "yyyy-MM-dd"
        licensedate = parameters.get("LICENSEDATE").toString(custom_format)
        print(f"许可到期时间:{licensedate}")
        licensehost = parameters.get("LICENSEHOST")
        print(f"许可计算机机器名:{licensehost}")
        params = {
            "host": licensehost,
            "license": licensedate
        }
        # 生成一个密钥
        key = Fernet.generate_key()
        # 使用密钥创建一个Fernet对象
        cipher_suite = Fernet(key)
        # 需要加密的数据
        message = str(params).encode("utf-8")
        #
        # # 加密数据
        encrypted_message = cipher_suite.encrypt(message)
        print(f"加密的消息: {encrypted_message}")
        # 要写入的文本内容
        text_lines = [key.decode("utf-8"), encrypted_message.decode("utf-8")]
        # 打开文件进行写入
        with open(outputpath, 'w') as file:
            # 遍历文本行并写入文件
            for line in text_lines:
                file.write(line + "\n")  # 添加换行符

    def getConsoleCommands(self, parameters, context, feedback, executing=True):
        print(parameters)
        arguments = [

        ]
        arguments = [

        ]

        return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
