# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '/home/yuh/code/my/ldr-translator/gui/qt/ui_preferences.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from PyQt5 import QtGui
from PyQt5.QtCore import QSize, QCoreApplication, Qt
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QWidget, QTabWidget, QCheckBox, QLabel, QComboBox, QPushButton, \
    QLineEdit, QSpacerItem, QSizePolicy, QMainWindow

from api import server_config
from api import translate
from api.server import baidu, tencent
from api.server_config import server_baidu, server_tencent, dict_api_save, get_api_key
from qt_utils import MyThread
from utils import config, version


# MainWindow.setWindowFlags(Qt.WindowStaysOnTopHint)

# icon = QtGui.QIcon()
# icon.addPixmap(QtGui.QPixmap("icon/icon.svg"))
# MainWindow.setWindowIcon(icon)


def update_autostart(a):
    config.update_autostart(a)


def update_ocr_local(a):
    config.set_ocr_local(a)


class UiMainWindow(QMainWindow):

    def setup_ui(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(380, 336)
        MainWindow.setWindowFlags(Qt.WindowStaysOnTopHint)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("icon/icon.svg"))
        MainWindow.setWindowIcon(icon)

        self.tw = QTabWidget(MainWindow)
        self.tw.setObjectName(u"tw")
        self.tab_normal = QWidget()
        self.tab_normal.setObjectName(u"tab_normal")
        self.vl_normal = QVBoxLayout(self.tab_normal)
        self.vl_normal.setSpacing(6)
        self.vl_normal.setObjectName(u"vl_normal")
        self.vl_normal.setContentsMargins(8, 8, 8, 8)
        self.cb_start_up = QCheckBox(self.tab_normal)
        self.cb_start_up.setObjectName(u"cb_start_up")

        self.vl_normal.addWidget(self.cb_start_up)

        self.cb_ocr_local = QCheckBox(self.tab_normal)
        self.cb_ocr_local.setObjectName(u"cb_ocr_local")

        self.vl_normal.addWidget(self.cb_ocr_local)

        self.lb_ocr_local_msg = QLabel(self.tab_normal)
        self.lb_ocr_local_msg.setObjectName(u"lb_ocr_local_msg")
        self.lb_ocr_local_msg.setMaximumSize(QSize(16777215, 36))

        self.vl_normal.addWidget(self.lb_ocr_local_msg)

        self.horizontalLayout_2 = QHBoxLayout()
        self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
        self.label_2 = QLabel(self.tab_normal)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setMaximumSize(QSize(16777215, 16777215))

        self.horizontalLayout_2.addWidget(self.label_2)

        self.cbb_tray_icon = QComboBox(self.tab_normal)
        self.cbb_tray_icon.setObjectName(u"cbb_tray_icon")
        self.cbb_tray_icon.setMaximumSize(QSize(16777215, 16777215))

        self.horizontalLayout_2.addWidget(self.cbb_tray_icon)

        self.label_3 = QLabel(self.tab_normal)
        self.label_3.setObjectName(u"label_3")

        self.horizontalLayout_2.addWidget(self.label_3)

        self.horizontalSpacer = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                            QSizePolicy.Minimum)

        self.horizontalLayout_2.addItem(self.horizontalSpacer)

        self.vl_normal.addLayout(self.horizontalLayout_2)

        self.label = QLabel(self.tab_normal)
        self.label.setObjectName(u"label")

        self.vl_normal.addWidget(self.label)

        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.btn_update = QPushButton(self.tab_normal)
        self.btn_update.setObjectName(u"btn_update")
        self.btn_update.setMaximumSize(QSize(72, 16777215))

        self.horizontalLayout.addWidget(self.btn_update)

        self.lb_version_code = QLabel(self.tab_normal)
        self.lb_version_code.setObjectName(u"lb_version_code")
        self.lb_version_code.setMaximumSize(QSize(16777215, 32))

        self.horizontalLayout.addWidget(self.lb_version_code)

        self.vl_normal.addLayout(self.horizontalLayout)

        self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum,
                                          QSizePolicy.Expanding)

        self.vl_normal.addItem(self.verticalSpacer)

        self.tw.addTab(self.tab_normal, "")
        self.tab_baidu = QWidget()
        self.tab_baidu.setObjectName(u"tab_baidu")
        self.vl_baidu = QVBoxLayout(self.tab_baidu)
        self.vl_baidu.setObjectName(u"vl_baidu")
        self.vl_baidu.setContentsMargins(8, 8, 8, 8)
        self.lb_bdt = QLabel(self.tab_baidu)
        self.lb_bdt.setObjectName(u"lb_bdt")
        self.lb_bdt.setMinimumSize(QSize(0, 18))
        self.lb_bdt.setMaximumSize(QSize(16777215, 18))

        self.vl_baidu.addWidget(self.lb_bdt)

        self.hl_bdt_app_id = QHBoxLayout()
        self.hl_bdt_app_id.setObjectName(u"hl_bdt_app_id")
        self.lb_bdt_app_id = QLabel(self.tab_baidu)
        self.lb_bdt_app_id.setObjectName(u"lb_bdt_app_id")
        self.lb_bdt_app_id.setMinimumSize(QSize(60, 18))
        self.lb_bdt_app_id.setMaximumSize(QSize(60, 18))

        self.hl_bdt_app_id.addWidget(self.lb_bdt_app_id)

        self.le_bdt_app_id = QLineEdit(self.tab_baidu)
        self.le_bdt_app_id.setObjectName(u"le_bdt_app_id")
        self.le_bdt_app_id.setMinimumSize(QSize(200, 30))
        self.le_bdt_app_id.setMaximumSize(QSize(16777215, 30))

        self.hl_bdt_app_id.addWidget(self.le_bdt_app_id)

        self.vl_baidu.addLayout(self.hl_bdt_app_id)

        self.hl_bdt_secret_key = QHBoxLayout()
        self.hl_bdt_secret_key.setObjectName(u"hl_bdt_secret_key")
        self.hl_bdt_secret_key.setContentsMargins(-1, -1, 0, -1)
        self.lb_bdt_secret_key = QLabel(self.tab_baidu)
        self.lb_bdt_secret_key.setObjectName(u"lb_bdt_secret_key")
        self.lb_bdt_secret_key.setMinimumSize(QSize(60, 18))
        self.lb_bdt_secret_key.setMaximumSize(QSize(60, 18))

        self.hl_bdt_secret_key.addWidget(self.lb_bdt_secret_key)

        self.le_bdt_secret_key = QLineEdit(self.tab_baidu)
        self.le_bdt_secret_key.setObjectName(u"le_bdt_secret_key")
        self.le_bdt_secret_key.setMinimumSize(QSize(200, 30))
        self.le_bdt_secret_key.setMaximumSize(QSize(16777215, 30))

        self.hl_bdt_secret_key.addWidget(self.le_bdt_secret_key)

        self.vl_baidu.addLayout(self.hl_bdt_secret_key)

        self.hl_bdt_save = QHBoxLayout()
        self.hl_bdt_save.setObjectName(u"hl_bdt_save")
        self.btn_bdt_save = QPushButton(self.tab_baidu)
        self.btn_bdt_save.setObjectName(u"btn_bdt_save")
        self.btn_bdt_save.setMaximumSize(QSize(66, 16777215))

        self.hl_bdt_save.addWidget(self.btn_bdt_save)

        self.lb_bdt_msg = QLabel(self.tab_baidu)
        self.lb_bdt_msg.setObjectName(u"lb_bdt_msg")

        self.hl_bdt_save.addWidget(self.lb_bdt_msg)

        self.hp_bdt_save = QSpacerItem(40, 18, QSizePolicy.Expanding,
                                       QSizePolicy.Minimum)

        self.hl_bdt_save.addItem(self.hp_bdt_save)

        self.lb_bdt_how = QLabel(self.tab_baidu)
        self.lb_bdt_how.setObjectName(u"lb_bdt_how")

        self.hl_bdt_save.addWidget(self.lb_bdt_how)

        self.vl_baidu.addLayout(self.hl_bdt_save)

        self.lb_bdo = QLabel(self.tab_baidu)
        self.lb_bdo.setObjectName(u"lb_bdo")
        self.lb_bdo.setMinimumSize(QSize(0, 18))
        self.lb_bdo.setMaximumSize(QSize(16777215, 18))

        self.vl_baidu.addWidget(self.lb_bdo)

        self.hl_bdo_api_key = QHBoxLayout()
        self.hl_bdo_api_key.setObjectName(u"hl_bdo_api_key")
        self.lb_bdo_api_key = QLabel(self.tab_baidu)
        self.lb_bdo_api_key.setObjectName(u"lb_bdo_api_key")
        self.lb_bdo_api_key.setMinimumSize(QSize(60, 18))
        self.lb_bdo_api_key.setMaximumSize(QSize(60, 18))

        self.hl_bdo_api_key.addWidget(self.lb_bdo_api_key)

        self.le_bdo_api_key = QLineEdit(self.tab_baidu)
        self.le_bdo_api_key.setObjectName(u"le_bdo_api_key")
        self.le_bdo_api_key.setMinimumSize(QSize(200, 30))
        self.le_bdo_api_key.setMaximumSize(QSize(16777215, 30))

        self.hl_bdo_api_key.addWidget(self.le_bdo_api_key)

        self.vl_baidu.addLayout(self.hl_bdo_api_key)

        self.hl_bdo_secret_key = QHBoxLayout()
        self.hl_bdo_secret_key.setObjectName(u"hl_bdo_secret_key")
        self.lb_bdo_secret_key = QLabel(self.tab_baidu)
        self.lb_bdo_secret_key.setObjectName(u"lb_bdo_secret_key")
        self.lb_bdo_secret_key.setMinimumSize(QSize(60, 18))
        self.lb_bdo_secret_key.setMaximumSize(QSize(60, 18))

        self.hl_bdo_secret_key.addWidget(self.lb_bdo_secret_key)

        self.le_bdo_secret_key = QLineEdit(self.tab_baidu)
        self.le_bdo_secret_key.setObjectName(u"le_bdo_secret_key")
        self.le_bdo_secret_key.setMinimumSize(QSize(200, 30))
        self.le_bdo_secret_key.setMaximumSize(QSize(16777215, 30))

        self.hl_bdo_secret_key.addWidget(self.le_bdo_secret_key)

        self.vl_baidu.addLayout(self.hl_bdo_secret_key)

        self.hl_bdo_save = QHBoxLayout()
        self.hl_bdo_save.setObjectName(u"hl_bdo_save")
        self.btn_bdo_save = QPushButton(self.tab_baidu)
        self.btn_bdo_save.setObjectName(u"btn_bdo_save")
        self.btn_bdo_save.setMaximumSize(QSize(66, 16777215))

        self.hl_bdo_save.addWidget(self.btn_bdo_save)

        self.lb_bdo_msg = QLabel(self.tab_baidu)
        self.lb_bdo_msg.setObjectName(u"lb_bdo_msg")

        self.hl_bdo_save.addWidget(self.lb_bdo_msg)

        self.hp_bdo_save = QSpacerItem(40, 18, QSizePolicy.Expanding,
                                       QSizePolicy.Minimum)

        self.hl_bdo_save.addItem(self.hp_bdo_save)

        self.lb_bdo_how = QLabel(self.tab_baidu)
        self.lb_bdo_how.setObjectName(u"lb_bdo_how")

        self.hl_bdo_save.addWidget(self.lb_bdo_how)

        self.vl_baidu.addLayout(self.hl_bdo_save)

        self.tw.addTab(self.tab_baidu, "")
        self.tab_tencent = QWidget()
        self.tab_tencent.setObjectName(u"tab_tencent")
        self.vl_tencent = QVBoxLayout(self.tab_tencent)
        self.vl_tencent.setObjectName(u"vl_tencent")
        self.vl_tencent.setContentsMargins(8, 8, 8, 8)
        self.lb_tct = QLabel(self.tab_tencent)
        self.lb_tct.setObjectName(u"lb_tct")
        self.lb_tct.setMinimumSize(QSize(0, 18))
        self.lb_tct.setMaximumSize(QSize(16777215, 18))

        self.vl_tencent.addWidget(self.lb_tct)

        self.hl_tct_secret_id = QHBoxLayout()
        self.hl_tct_secret_id.setObjectName(u"hl_tct_secret_id")
        self.lb_tct_secret_id = QLabel(self.tab_tencent)
        self.lb_tct_secret_id.setObjectName(u"lb_tct_secret_id")
        self.lb_tct_secret_id.setMinimumSize(QSize(60, 18))
        self.lb_tct_secret_id.setMaximumSize(QSize(60, 18))

        self.hl_tct_secret_id.addWidget(self.lb_tct_secret_id)

        self.le_tct_secret_id = QLineEdit(self.tab_tencent)
        self.le_tct_secret_id.setObjectName(u"le_tct_secret_id")
        self.le_tct_secret_id.setMinimumSize(QSize(200, 30))
        self.le_tct_secret_id.setMaximumSize(QSize(16777215, 30))

        self.hl_tct_secret_id.addWidget(self.le_tct_secret_id)

        self.vl_tencent.addLayout(self.hl_tct_secret_id)

        self.hl_tct_secret_key = QHBoxLayout()
        self.hl_tct_secret_key.setObjectName(u"hl_tct_secret_key")
        self.hl_tct_secret_key.setContentsMargins(-1, -1, 0, -1)
        self.lb_tct_secret_key = QLabel(self.tab_tencent)
        self.lb_tct_secret_key.setObjectName(u"lb_tct_secret_key")
        self.lb_tct_secret_key.setMinimumSize(QSize(60, 18))
        self.lb_tct_secret_key.setMaximumSize(QSize(60, 18))

        self.hl_tct_secret_key.addWidget(self.lb_tct_secret_key)

        self.le_tct_secret_key = QLineEdit(self.tab_tencent)
        self.le_tct_secret_key.setObjectName(u"le_tct_secret_key")
        self.le_tct_secret_key.setMinimumSize(QSize(200, 30))
        self.le_tct_secret_key.setMaximumSize(QSize(16777215, 30))

        self.hl_tct_secret_key.addWidget(self.le_tct_secret_key)

        self.vl_tencent.addLayout(self.hl_tct_secret_key)

        self.hl_tct_save = QHBoxLayout()
        self.hl_tct_save.setObjectName(u"hl_tct_save")
        self.btn_tct_save = QPushButton(self.tab_tencent)
        self.btn_tct_save.setObjectName(u"btn_tct_save")
        self.btn_tct_save.setMaximumSize(QSize(66, 16777215))

        self.hl_tct_save.addWidget(self.btn_tct_save)

        self.lb_tct_msg = QLabel(self.tab_tencent)
        self.lb_tct_msg.setObjectName(u"lb_tct_msg")

        self.hl_tct_save.addWidget(self.lb_tct_msg)

        self.hp_tct_save = QSpacerItem(40, 18, QSizePolicy.Expanding,
                                       QSizePolicy.Minimum)

        self.hl_tct_save.addItem(self.hp_tct_save)

        self.lb_tct_how = QLabel(self.tab_tencent)
        self.lb_tct_how.setObjectName(u"lb_tct_how")

        self.hl_tct_save.addWidget(self.lb_tct_how)

        self.vl_tencent.addLayout(self.hl_tct_save)

        self.verticalSpacer_2 = QSpacerItem(20, 40, QSizePolicy.Minimum,
                                            QSizePolicy.Expanding)

        self.vl_tencent.addItem(self.verticalSpacer_2)

        self.tw.addTab(self.tab_tencent, "")
        MainWindow.setCentralWidget(self.tw)

        self.retranslateUi(MainWindow)

        self.tw.setCurrentIndex(0)

    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(
            QCoreApplication.translate("MainWindow", u"MainWindow", None))
        self.cb_start_up.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u5f00\u673a\u81ea\u542f", None))
        self.cb_ocr_local.setText(
            QCoreApplication.translate(
                "MainWindow", u"\u79bb\u7ebf\u6587\u672c\u8bc6\u522b(ocr)",
                None))
        self.lb_ocr_local_msg.setText(
            QCoreApplication.translate(
                "MainWindow",
                u"\u79bb\u7ebfocr\uff0c\u9700\u624b\u52a8\u5b89\u88c5\u4f9d\u8d56\uff1apip install easyocr",
                None))
        self.label_2.setText(
            QCoreApplication.translate(
                "MainWindow", u"\u901a\u77e5\u680f\u56fe\u6807\uff1a", None))
        self.label_3.setText(
            QCoreApplication.translate(
                "MainWindow", u"\u91cd\u65b0\u6253\u5f00\u540e\u751f\u6548",
                None))
        self.label.setText("")
        self.btn_update.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u68c0\u67e5\u66f4\u65b0", None))
        self.lb_version_code.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u5f53\u524d\u7248\u672c\uff1a",
                                       None))
        self.tw.setTabText(
            self.tw.indexOf(self.tab_normal),
            QCoreApplication.translate("MainWindow", u"\u8bbe\u7f6e", None))
        self.lb_bdt.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u767e\u5ea6\u7ffb\u8bd1api", None))
        self.lb_bdt_app_id.setText(
            QCoreApplication.translate("MainWindow", u"AppId", None))
        self.lb_bdt_secret_key.setText(
            QCoreApplication.translate("MainWindow", u"SecretKey", None))
        self.btn_bdt_save.setText(
            QCoreApplication.translate("MainWindow", u"\u4fdd\u5b58", None))
        self.lb_bdt_msg.setText("")
        self.lb_bdt_how.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u5982\u4f55\u83b7\u53d6", None))
        self.lb_bdo.setText(
            QCoreApplication.translate(
                "MainWindow", u"\u767e\u5ea6\u56fe\u7247\u8bc6\u522bapi",
                None))
        self.lb_bdo_api_key.setText(
            QCoreApplication.translate("MainWindow", u"ApiKey", None))
        self.lb_bdo_secret_key.setText(
            QCoreApplication.translate("MainWindow", u"SecretKey", None))
        self.btn_bdo_save.setText(
            QCoreApplication.translate("MainWindow", u"\u4fdd\u5b58", None))
        self.lb_bdo_msg.setText("")
        self.lb_bdo_how.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u5982\u4f55\u83b7\u53d6", None))
        self.tw.setTabText(
            self.tw.indexOf(self.tab_baidu),
            QCoreApplication.translate("MainWindow", u"\u767e\u5ea6Api", None))
        self.lb_tct.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u817e\u8baf\u7ffb\u8bd1api", None))
        self.lb_tct_secret_id.setText(
            QCoreApplication.translate("MainWindow", u"SecretId", None))
        self.lb_tct_secret_key.setText(
            QCoreApplication.translate("MainWindow", u"SecretKey", None))
        self.btn_tct_save.setText(
            QCoreApplication.translate("MainWindow", u"\u4fdd\u5b58", None))
        self.lb_tct_msg.setText("")
        self.lb_tct_how.setText(
            QCoreApplication.translate("MainWindow",
                                       u"\u5982\u4f55\u83b7\u53d6", None))
        self.tw.setTabText(
            self.tw.indexOf(self.tab_tencent),
            QCoreApplication.translate("MainWindow", u"\u817e\u8bafApi", None))
        # retranslateUi

        self.load_data()
        self._listen()

    # self.load_data()
    # self._listen()

    # retranslateUi

    def load_data(self):

        def get_value(tv, key_0, server_=server_config.server_baidu):
            tv.setText(config.get_value(server_, key_0))

        dict_how = {
            self.lb_bdt_how: baidu.how_get_url_translate,
            self.lb_bdo_how: baidu.how_get_url_ocr,
            self.lb_tct_how: tencent.how_get_url_translate,
        }

        for key in dict_how:
            key.setOpenExternalLinks(True)
            key.setText("<a href='%s'>如何获取？</a>" % dict_how[key])

        dict_le = {
            self.le_bdt_app_id: [server_baidu, "ta"],
            self.le_bdt_secret_key: [server_baidu, "tb"],
            self.le_bdo_api_key: [server_baidu, "oa"],
            self.le_bdo_secret_key: [server_baidu, "ob"],
            self.le_tct_secret_id: [server_tencent, "ta"],
            self.le_tct_secret_key: [server_tencent, "tb"],
        }

        for key in dict_le:
            server = dict_le[key][0]
            key_ = dict_le[key][1]
            key_config = dict_api_save[server][key_]
            get_value(key, key_config, server)

        self.lb_version_code.setText(version.get_default())
        self.cb_start_up.setChecked(config.get_autostart())
        self.cb_ocr_local.setChecked(config.is_ocr_local())
        self.lb_version_code.setOpenExternalLinks(True)

        tray_types = config.get_tray_types()
        self.cbb_tray_icon.addItems(tray_types)
        self.cbb_tray_icon.setCurrentText(tray_types[config.get_tray_icon_n()])
        self.cbb_tray_icon.currentIndexChanged.connect(self.on_cbb_tray_icon)

    def _listen(self):
        self.btn_bdt_save.clicked.connect(self.save_bdt)
        self.btn_bdo_save.clicked.connect(self.save_bdo)
        self.btn_tct_save.clicked.connect(self.save_tct)
        self.btn_update.clicked.connect(self.check_update)
        self.cb_start_up.clicked.connect(update_autostart)
        self.cb_ocr_local.clicked.connect(update_ocr_local)

    def on_cbb_tray_icon(self):
        config.set_tray_icon(self.cbb_tray_icon.currentText())

    def check_update(self, view=None):

        def set_msg(res):
            s, msg = res
            # self.lb_version_msg.setText(msg)
            self.lb_version_code.setText(s)

        self.lb_version_code.setText("检查中...")
        self.thread = MyThread(version.check_update)
        self.thread.signal.connect(set_msg)
        self.thread.start()

    def save_bdt(self, btn):
        self.save_server(self.le_bdt_app_id, self.le_bdt_secret_key,
                         self.lb_bdt_msg, server_baidu)

    def save_bdo(self, btn):
        self.save_server(self.le_bdo_api_key, self.le_bdo_secret_key,
                         self.lb_bdo_msg, server_baidu, True)

    def save_tct(self, btn):
        self.save_server(self.le_tct_secret_id, self.le_tct_secret_key,
                         self.lb_tct_msg, server_tencent)

    def save_server(self, tv_a, tv_b, lb_msg, server, is_ocr=False):

        text_a = tv_a.text()
        text_b = tv_b.text()

        def set_ui(result):
            ok, text_a_, text_b_ = result
            print(ok, text_a_, text_b_)
            msg = "超时或账号密码错误"
            tv_a.setText(text_a_)
            tv_b.setText(text_b_)
            if ok:
                msg = "成功，已保存"
                key_a, key_b = get_api_key(server, is_ocr)
                config.set_config(server, key_a, text_a_)
                config.set_config(server, key_b, text_b_)
            lb_msg.setText(msg)

        if len(text_a) == 0 or len(text_b) == 0:
            lb_msg.setText("已恢复默认（不推荐）")
        else:
            lb_msg.setText("加载中...")

            self.thread = MyThread(translate.check_server_api,
                                   (is_ocr, server, text_a, text_b))
            self.thread.signal.connect(set_ui)
            self.thread.start()
