#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from sys import argv
from utils.globalvar import SD
from widget.bms_login import Win_Login
from controller.bms_home_ctl import BmsHomeControll
from utils.qt import QApplication

class BmsLoginControll:
    def __init__(self):
        # 不需要从命令行输入参数
        self._app = QApplication(argv)
        self._view = Win_Login()
        self.init()

    def init(self):
        self._view.login_signal.connect(self._verify_user)
        self._view.cancel_signal.connect(self._login_cancel)
        self._view.home_show_signal.connect(self._show_main)
        self._view.language_signal.connect(self._change_language)

    def _verify_user(self):
        password = self._view.edt_password.text().strip()
        if self._view.cb_role.currentIndex() == 0:
            self._view.on_success()
        elif self._view.cb_role.currentIndex() == 1:
            if password == "embmstest":
                self._view.on_success()
            else:
                self._view.on_error()
        elif self._view.cb_role.currentIndex() == 2:
            if password == "embms":
                self._view.on_success()
            else:
                self._view.on_error()
        elif self._view.cb_role.currentIndex() == 3:
            if password == "embmspack":
                self._view.on_success()
            else:
                self._view.on_error()
        elif self._view.cb_role.currentIndex() == 4:
            if password == "embmsAdministrator":
                self._view.on_success()
            else:
                self._view.on_error()

    def _show_main(self):
        self.home = BmsHomeControll()
        self.home.run()

    def _login_cancel(self):
        self._view.close()

    def _change_language(self, index):
        SD.SYSTEM_LANGUAGE = index
        self._view.setWindowTitle("用户登录" if SD.SYSTEM_LANGUAGE == 0 else "Login")
        self._view.lb_role.setText("登录角色" if SD.SYSTEM_LANGUAGE == 0 else "Role")
        self._view.lb_password.setText("登录密码" if SD.SYSTEM_LANGUAGE == 0 else "PassWord")
        self._view.lb_prj.setText("登录项目" if SD.SYSTEM_LANGUAGE == 0 else "Project")
        self._view.lb_con_type.setText("通讯类型" if SD.SYSTEM_LANGUAGE == 0 else "Conn Type")
        self._view.lb_channel.setText("索引号" if SD.SYSTEM_LANGUAGE == 0 else "Index")
        self._view.lb_index.setText("通道号" if SD.SYSTEM_LANGUAGE == 0 else "Channel")
        self._view.lb_baudrate.setText("波特率" if SD.SYSTEM_LANGUAGE == 0 else "BPS")
        self._view.edt_password.setPlaceholderText("密码" if SD.SYSTEM_LANGUAGE == 0 else "PassWord")
        self._view.btn_login.setText("登录" if SD.SYSTEM_LANGUAGE == 0 else "Login")
        self._view.btn_cancel.setText("取消" if SD.SYSTEM_LANGUAGE == 0 else "Cancel")
        self._view.cb_role.setItemText(0, "普通用户" if SD.SYSTEM_LANGUAGE == 0 else "Normal")
        self._view.cb_role.setItemText(1, "调试工程师" if SD.SYSTEM_LANGUAGE == 0 else "Test")
        self._view.cb_role.setItemText(2, "BMS产线操作员" if SD.SYSTEM_LANGUAGE == 0 else "Em")
        self._view.cb_role.setItemText(3, "PACK产线操作" if SD.SYSTEM_LANGUAGE == 0 else "Pack")
        self._view.cb_role.setItemText(4, "系统管理员" if SD.SYSTEM_LANGUAGE == 0 else "Manager")
        self._view.cb_prj.setItemText(0, "户储" if SD.SYSTEM_LANGUAGE == 0 else "Home")
        self._view.cb_prj.setItemText(1, "工商业" if SD.SYSTEM_LANGUAGE == 0 else "Industry")

    def run(self):
        self._view.show()
        return self._app.exec_()
