import os
import sys

from PySide6.QtWidgets import QMainWindow, QLabel, QMessageBox

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = os.path.dirname(CURRENT_DIR)
if PROJECT_ROOT not in sys.path:
    sys.path.insert(0, PROJECT_ROOT)

from window.Main import Ui_MainWindow
from bsphp.main import BsPhp

class Main(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None, bs_php: BsPhp = None, end_time: str = ''):
        super(Main, self).__init__(parent)
        self.setupUi(self)
        self.end_time = end_time
        self.bsPhp = bs_php
        self.show()
        self.label_desc.setText(self.bsPhp.get_soft_info())
        self.status_time = QLabel("到期时间:"+end_time)
        self.status_bind = QLabel("绑定特征:"+self.bsPhp.get_bind())
        self.status_server_time = QLabel("服务器时间:"+self.bsPhp.get_server_time())
        self.status_version = QLabel("版本号:"+self.bsPhp.get_version())
        self.statusBar.addWidget(self.status_time)
        self.statusBar.addWidget(self.status_bind)
        self.statusBar.addWidget(self.status_server_time)
        self.statusBar.addWidget(self.status_version)

    def onTestConnect(self):
        """测试连接"""
        self.message_box('连接成功！' if self.bsPhp.connect() else '连接失败！')

    def onEndTime(self):
        self.message_box(self.bsPhp.get_end_time())

    def onBind(self):
        self.message_box(self.bsPhp.get_bind())

    def onOpenUrl(self):
        os.system('start ' + self.bsPhp.get_verify())

    def onUserInfo(self):
        self.message_box(self.bsPhp.get_userinfo('信息字段：#激活时间>UserReDate#激活时Ip>UserReIp#用户状态>UserIsLock#登录时间>UserLogInDate#登录Ip>UserLogInIp#到期时>UserVipDate#绑定特征>UserKey#用户分组名称>Class_Nane#用户分组别名>Class_Mark##用户QQ>UserQQ#用户邮箱>UserMAIL#购卡折扣>UserPayZhe#是否代理代理>1>UserTreasury#电话>UserMobile#帐号金额>UserRMB#帐号积分>UserPoint#密保问题>Usermibao_wenti##VIP到期时间>UserVipDate#vip是否到期没有到期返回1到期返回2>UserVipWhether').replace('#','\n'))

    def onVerify(self):
        self.message_box(self.bsPhp.get_verify())

    def onLoginStatus(self):
        self.message_box('已登录' if self.bsPhp.get_login_status()== '1' else '未登录')

    def onSoftInfo(self):
        self.message_box(self.bsPhp.get_soft_info())


    def message_box(self, info: str, button=QMessageBox.Yes):
        """
        信息框
        :param info: 内容
        :param button: 按钮
        :return:
        """
        return QMessageBox.information(None, "", info, button, QMessageBox.Yes)



