# coding: utf-8
# Author: sww
# Date: 2024/7/9
# 用于判断软件状态的类
import time, os
import public
from panelPlugin import Plugin


class main:
    panel_path = public.get_setup_path()
    nginx_path = os.path.join(panel_path, 'nginx')
    apache_path = os.path.join(panel_path, 'apache')
    mysql_path = os.path.join(panel_path, 'mysql')
    if os.path.exists(mysql_path) and os.listdir(mysql_path):
        mysql_path = os.path.join(mysql_path, os.listdir(mysql_path)[-1])
    filezilla_path = os.path.join(panel_path, 'ftpServer')
    
    def get_soft_status(self, get):
        """
        获取软件状态
        """
        try:
            data = {}
            name = get.name
            title_map = {'nginx': 'Nginx', 'apache': 'Apache', 'iis': 'IIS', 'mysql': 'Mysql', 'FileZilla Server': 'FileZilla Server'}
            if name == 'web':
                name = public.get_webserver()
                setup_iis_path = os.path.join(self.panel_path, 'data/iis.setup')
                if os.path.exists(setup_iis_path.replace('/', '\\')):
                    name = 'iis'
            data["status"] = public.get_server_status(name)  # （-2:安装中 ,-1：未安装 0：未启动 1：已启动）
            if name == None:
                # 获取正在安装的任务
                for name1 in ['iis', 'apache', 'nginx']:
                    res = public.M('tasks').where('status=? and name like ?', (-1, "%{}-%".format(name1))).select()
                    if res:
                        data['status'] = -2
                        data["res"] = res
                        name = name1
                        break
            data["title"] = title_map.get(name, name)
            version_map = {
                'nginx': os.path.join(self.nginx_path, 'version.pl'),
                'apache': os.path.join(self.apache_path, 'version.pl'),
                'mysql': os.path.join(self.mysql_path, 'version.pl'),
                'FileZilla Server': os.path.join(self.filezilla_path, 'version.pl'),
                "iis": os.path.join(self.nginx_path, 'version.pl')
            }
            if name == 'iis':
                data['version'] = self.get_iis_version()
                data['status'] = public.get_server_status('w3svc')
            else:
                try:
                    data['version'] = public.readFile(version_map[name].replace('/', '\\'))
                    if data['version'] == False:
                        data['version'] = ''
                except:
                    data['version'] = ''
            return data
        except:
            return public.get_error_info()
    
    def get_iis_version(self):
        try:
            import winreg
            # 打开注册表项
            key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\InetStp")
            
            # 读取VersionString值
            version_string, _ = winreg.QueryValueEx(key, "VersionString")
            
            # 关闭注册表项
            winreg.CloseKey(key)
            version_string = version_string.split(' ')[-1]
            return version_string
        except:
            return False
