from Business.Class.ExcelUtils import *
from Business.Class.JsonOrYaml import *
from Base.Class.Http import *

from Base.Debug.Decorator import *
import pandas as pd
from pandas.core.frame import DataFrame


class RunTest:
    # 全局变量列表
    _g_ = {}
    # 局部变量列表
    _l_ = {}
    _r_ = None
    # 日志报告输出路径
    fileLst = {"log": None, "report": None}
    # 执行用例级别列表
    caseLvl = None
    viewLst = None
    cellLst = None

    def __init__(self, *args, **kwargs):
        self._init(*args, **kwargs)

    def _setRequestMode(self, mode):
        self._r_ = [Request, Session][mode]()
        return True

    def _setOutput(self, **kwargs):
        import os
        for key in kwargs:
            self.fileLst[key] = kwargs[key] and os.path.abspath(kwargs[key])
        return True

    def _matLevelThatRun(self, lv=None):
        if lv is None:
            return False
        if self.caseLvl is None:
            return True
        return lv in self.caseLvl

    def _init(self, workbook, data='数据配置', case='测试用例', level=None, log=None, report=None):
        init = Excel().open(workbook, read_only=True)
        init_data = auto_decode(init.select(data).cellGet(0, 0))
        init_case = auto_decode(init.select(case).cellGet(0, 0))
        self.cellLst = {
            "ITEM":
                auto_decode(init.select(case).cellGet(cell=init_case["fixeds"]["item"])),
            "MODE":
                auto_decode(init.select(case).cellGet(cell=init_case["fixeds"]["mode"]))
        }
        self.viewLst = {
            "HTTP":
                DataFrame(read_view_dict(filename=workbook, sheet=data,
                area=init_data["tables"]["http"]["views"], fields=init_data["tables"]["http"]["field"],
                auto_truncate=True)),
            "BASE":
                DataFrame(read_view_dict(filename=workbook, sheet=data,
                area=init_data["tables"]["base"]["views"],
                fields=init_data["tables"]["base"]["field"],
                auto_truncate=True)),
            "DATA":
                DataFrame(read_view_dict(filename=workbook, sheet=data,
                area=init_data["tables"]["data"]["views"],
                fields=init_data["tables"]["data"]["field"],
                auto_truncate=True)),
            "CASE":
                DataFrame(read_view_dict(filename=workbook, sheet=case,
                area=init_case["tables"]["case"]["views"],
                fields=init_case["tables"]["case"]["field"],
                auto_truncate=True)),
        }
        init.exit()
        self.levels(level)
        self.report(log=log, report=report)
        self._setRequestMode(self.cellLst["MODE"] in ['会话模式', 'Session'])
        return True

    def _run_mini(self, case_dict):
        print(case_dict)













    def report(self, log=None, report=None):
        self._setOutput(log=log, report=report)
        return self

    def levels(self, level=None):
        if isinstance(level, (list, tuple, str)):
            self.caseLvl = level
        return self



if __name__ == '__main__':
    r = RunTest('D:/Desktop/接口自动化测试用例.xlsx','数据配置','测试用例', level=['P0'], log='./1.txt', report='./1.html')
    r._run_mini(r.viewLst["CASE"].loc[0].to_dict())
























# print(tables)
# print(json_encode(tables, indent=4, unicode=False))
# print(json_encode(tables["HTTP"], indent=4, unicode=False))
# data = tables["HTTP"]
# data = data.where((data["Env"] == '测试环境') & (data["Name"] == 'HTTP0'), inplace=False).dropna(how='all')
# print(data.loc[:,"Name"])