# -*- coding:utf-8 -*-
from UAT_log import info,debug,error
from UAT_tree import FirstLevel,NLevel,DialogLevel,UATTree

import os, sys, time
import xlrd# 字典不排序;
import json


DEBUG = True
INFO = True
ERROR = True
class UATExcelParser():
    cls = "UATExcelParser"
    def __init__(self, uiTree):
        self.levelList=[]
        self.uiTree = uiTree
        self.eventKeyCode = {}

    def read_excel(self, path=None):
        debug(self.cls, "read_excel", "path:"+path, DEBUG)
        if path is not None:
            if type(path) == str:
                path = path.decode('utf-8')
            self.xls_path = path

        # 打开文件;
        wb = xlrd.open_workbook(filename=self.xls_path)
        if wb is None:
            error(self.cls, "read_excel","文件打开失败!"+path, ERROR)
            return
        # 获取所有sheet;
        sheet = None
        self.levelList = wb.sheet_names()
        if "dialog" in self.levelList:
            sheet = wb.sheet_by_name("dialog")
            self.parse_excel(sheet, False)
            self.levelList.remove("dialog")
        for sh_name in self.levelList:
            debug(self.cls, "read_excel", "sheet_name="+sh_name, DEBUG)
            sheet = wb.sheet_by_name(sh_name)
            self.parse_excel(sheet, False)

        # self.uiTree.printTree()

    def read_keyCode(self, path):
        if path is None:
            info(self.cls, "read_excel_keyCode", "未读取到相关的keyCode文件,以默认eventKey为准", INFO)
            return
        debug(self.cls, "read_excel_keyCode", "keycode_path:"+path, DEBUG)
        # 打开文件;
        path = path.decode('utf-8')
        wb = xlrd.open_workbook(filename=path)
        sheet = wb.sheet_by_index(0)
        self.parse_keyCode(sheet)


    "根据level sheet名字,确定在第几层"
    def getLevelNO(self, levelSheet):
        return self.levelList.index(levelSheet)

    # endfun

    def parse_excel(self, sheet, bpath=True):
        parentName = ""
        if u"first" == sheet.name.lower():
            for i in range(1, sheet.nrows):
                # 获取每行内容;
                oneRow = tuple(sheet.row_values(i))
                parentName = self.parseFLevelRow(oneRow, sheet.name, parentName)
        elif u"dialog" == sheet.name.lower():
            for i in range(1, sheet.nrows):
                # 获取每行内容;
                oneRow = tuple(sheet.row_values(i))
                parentName = self.parseDLevelRow(oneRow, parentName)
        else:  # 路径;
            for i in range(1, sheet.nrows):
                # 获取每行内容;
                oneRow = tuple(sheet.row_values(i))
                parentName = self.parseNLevelRow(oneRow, sheet.name, parentName)

    # endfun

    def parse_keyCode(self, sheet):
        for i in range(1, sheet.nrows):
            # 获取每行内容;
            oneRow = tuple(sheet.row_values(i))
            name = oneRow[0].upper()
            code = int(oneRow[1])
            self.eventKeyCode[name] = code

    #endfun

    def parseFLevelRow(self,oneRow, level, parentName):
        # debug(self.cls, "parseFLevelRow", "level:%s, parentName:%s"%(level, parentName), DEBUG)
        pn = oneRow[FirstLevel.ParentCol]
        if pn.__len__() > 0:
            self.uiTree.addParent(level.lower(), pn.lower(),
                                  oneRow[FirstLevel.ShortCutKeyCol],
                                  oneRow[FirstLevel.UIViewCol],
                                  oneRow[FirstLevel.MoveKeyCol],
                                  oneRow[FirstLevel.ToParentKeyCol],
                                  oneRow[FirstLevel.LayoutCol],
                                  oneRow[FirstLevel.OthersCol])
            return pn
        oname = oneRow[FirstLevel.OptionCol]
        if oname.__len__() > 0:
            self.uiTree.addOption(level.lower(), parentName.lower(), oname.lower(),
                                  oneRow[FirstLevel.OptionViewCol],
                                  oneRow[FirstLevel.FocusSelectCol],
                                  oneRow[FirstLevel.FocuseViewCol],
                                  oneRow[FirstLevel.EnterKeyCol],
                                  oneRow[FirstLevel.TextValueCol],
                                  oneRow[FirstLevel.InfoViewCol])
        return parentName

    def parseNLevelRow(self,oneRow, level, parentName):
        # debug(self.cls, "parseFLevelRow", "level:%s, parentName:%s"%(level, parentName), DEBUG)
        pn = oneRow[NLevel.ParentCol]
        if pn.__len__() > 0:
            self.uiTree.addParent(level.lower(), pn.lower(), "",
                                  oneRow[NLevel.UIViewCol],
                                  oneRow[NLevel.MoveKeyCol],
                                  oneRow[NLevel.ToParentKeyCol],
                                  oneRow[NLevel.LayoutCol],
                                  oneRow[NLevel.OthersCol])
            return pn
        oname = oneRow[NLevel.OptionCol]
        if oname.__len__() > 0:
            self.uiTree.addOption(level.lower(), parentName.lower(), oname.lower(),
                                  oneRow[NLevel.OptionViewCol],
                                  oneRow[NLevel.FocusSelectCol],
                                  oneRow[NLevel.FocuseViewCol],
                                  oneRow[NLevel.EnterKeyCol],
                                  oneRow[NLevel.TextValueCol],
                                  oneRow[NLevel.InfoViewCol])
        return parentName

    # 添加dialog
    def parseDLevelRow(self, oneRow, parentName):
        # debug(self.cls, "parseDLevelRow", "level:%s, parentName:%s"%(level, parentName), DEBUG)
        pn = oneRow[DialogLevel.DialogCol]
        if pn.__len__() > 0:
            self.uiTree.addDialog(pn.lower(), "",
                                  oneRow[DialogLevel.UIViewCol],
                                  oneRow[DialogLevel.MoveKeyCol],
                                  oneRow[DialogLevel.ToParentKeyCol],
                                  oneRow[DialogLevel.LayoutCol],
                                  oneRow[DialogLevel.OthersCol])
            return pn
        oname = oneRow[DialogLevel.OptionCol]
        if oname.__len__() > 0:
            self.uiTree.addDialogOption(parentName.lower(), oname.lower(),
                                  oneRow[DialogLevel.OptionViewCol],
                                  oneRow[DialogLevel.FocusSelectCol],
                                  oneRow[DialogLevel.FocuseViewCol],
                                  oneRow[DialogLevel.EnterKeyCol],
                                  oneRow[DialogLevel.TextValueCol],
                                  oneRow[DialogLevel.InfoViewCol])
        return parentName

    def parseParentDialog(self):
        self.uiTree.parseDialogParam()