# from browserUtil import browserUtil
import json
import webbrowser
from WebSocketServer import WebSocketServer


class BusinessHadler:
    def __init__(self):
        print('__init__ socket')
        self.websocket_server = WebSocketServer()
        self.websocket_server.start()

    def send_message_to_clients(self, message):
        self.websocket_server.sendToAllClients(message)

    def cleanup(self):
        print('cleanup')
        self.websocket_server.stop()

    def handler(self, intent_action, intent_solts):
        # 遍历词槽，提取并转换关键信息
        solts = {}
        for item in intent_solts:
            if item["name"] == "xs_date" or item["name"] == "plan_month" or item["name"] == "time" or item[
                "name"] == "start_time":
                timedata = json.loads(item["normValue"])
                solts[item["name"]] = timedata["datetime"]
            else:
                solts[item["name"]] = item["normValue"]
        print("解析后的intent_solts: ", solts)

        tts_text = ""

        # 打开系统网页
        if intent_action == "open_system":
            # 使用webbrowser.open，会阻塞程序，导致AIUI发送2次NLP回调，打开2次浏览器页面，需要在调用前断开信号
            webbrowser.open('http://101.37.148.192:8080?socket=1')

            # 使用以下方式打开，也可以避免打开2次web页面问题
            # import os
            # os.system('start http://101.37.148.192:8080/')
        # 返回主页面
        elif intent_action == "main_page":
            message = '{"pageName":"map","type":"page_change"}'
            self.send_message_to_clients(message)
        # 查看系统模块
        elif intent_action == "open_modules":
            print(solts)
            if solts['charts'] == "今日线下门户流量":
                pageName = "offline"
            elif solts['charts'] == "平均用户类别排布":
                pageName = "userIdentityCategory"
            elif solts['charts'] == "用户数据状态":
                pageName = "userSitua"
            elif solts['charts'] == "关联数据类别分布":
                pageName = "browseCategories"
            elif solts['charts'] == "今日流量态势":
                pageName = "trafficSitua"

            message = '{"pageName":"' + pageName + '","type":"page_change"}'
            print(message)

            self.send_message_to_clients(message)

        # 查询在线/离线用户数
        elif intent_action == "query_online_user":
            message = '{"type":"user_query","userDialog":{"date":"' + solts['time'] + '","status":"' + solts[
                'onlineState'] + '","number":"87"}}'
            print(message)

            self.send_message_to_clients(message)

        # 查询今日服务次数，并播报
        elif intent_action == "query_server_times":
            tts_text = '今日服务次数53461次 服务质量90%'
        else:
            print("无效意图")
            tts_text = "我没有理解您的话"

        print('handler end')
        return tts_text

    def handler_dipai(self, intent_action, intent_solts):

        solts = intent_solts[0]
        print("解析后的intent_solts: ", solts)
        tts_text = ""

        # 打开系统网页
        if intent_action == "open_system":
            # 使用webbrowser.open，会阻塞程序，导致AIUI发送2次NLP回调，打开2次浏览器页面，需要在调用前断开信号
            webbrowser.open('http://101.37.148.192:8080?socket=1')

            # 使用以下方式打开，也可以避免打开2次web页面问题
            # import os
            # os.system('start http://101.37.148.192:8080/')
        # 返回主页面
        elif intent_action == "main_page":
            message = '{"pageName":"map","type":"page_change"}'
            self.send_message_to_clients(message)
        # 查看系统模块
        elif intent_action == "open_modules":
            print(solts)
            if solts['slot_value'] == "今日线下门户流量":
                pageName = "offline"
            elif solts['slot_value'] == "平均用户类别排布":
                pageName = "userIdentityCategory"
            elif solts['slot_value'] == "用户数据状态":
                pageName = "userSitua"
            elif solts['slot_value'] == "关联数据类别分布":
                pageName = "browseCategories"
            elif solts['slot_value'] == "今日流量态势":
                pageName = "trafficSitua"
            elif solts['slot_value'] == "主界面":
                pageName = "map"

            message = '{"pageName":"' + pageName + '","type":"page_change"}'
            print(message)

            self.send_message_to_clients(message)

        # 查询在线/离线用户数
        elif intent_action == "query_online_user":
            message = '{"type":"user_query","userDialog":{"date":"' + solts['time'] + '","status":"' + solts[
                'onlineState'] + '","number":"87"}}'
            print(message)

            self.send_message_to_clients(message)

        # 查询今日服务次数，并播报
        elif intent_action == "query_server_times":
            tts_text = '今日服务次数53461次 服务质量90%'
        else:
            print("无效意图")
            tts_text = "我没有理解您的话"

        print('handler end')
        return tts_text

    def handler_grpc(self, intent_action, intent_solts):
        # 遍历词槽，提取并转换关键信息
        solts = intent_solts

        tts_text = ""

        # 打开系统网页
        if intent_action == "open_web":
            # 使用webbrowser.open，会阻塞程序，导致AIUI发送2次NLP回调，打开2次浏览器页面，需要在调用前断开信号
            webbrowser.open('http://101.37.148.192:8080?socket=1')

            # 使用以下方式打开，也可以避免打开2次web页面问题
            # import os
            # os.system('start http://101.37.148.192:8080/')
        # 返回主页面
        elif intent_action == "main_page":
            message = '{"pageName":"map","type":"page_change"}'
            self.send_message_to_clients(message)
        # 查看系统模块
        elif intent_action == "open_modules":
            print(solts)
            if solts['charts'] == "今日线下门户流量":
                pageName = "offline"
            elif solts['charts'] == "平均用户类别排布":
                pageName = "userIdentityCategory"
            elif solts['charts'] == "用户数据状态":
                pageName = "userSitua"
            elif solts['charts'] == "关联数据类别分布":
                pageName = "browseCategories"
            elif solts['charts'] == "今日流量态势":
                pageName = "trafficSitua"

            message = '{"pageName":"' + pageName + '","type":"page_change"}'
            print(message)

            self.send_message_to_clients(message)

        # 查询在线/离线用户数
        elif intent_action == "query_online_user":
            message = '{"type":"user_query","userDialog":{"date":"' + solts['time'] + '","status":"' + solts[
                'onlineState'] + '","number":"87"}}'
            print(message)

            self.send_message_to_clients(message)

        # 查询今日服务次数，并播报
        elif intent_action == "query_server_times":
            tts_text = '今日服务次数53461次 服务质量90%'
        else:
            print("无效意图")
            tts_text = "我没有理解您的话"

        print('handler end')
        return tts_text

    def handler_dipai(self, intent_action, intent_solts):

        solts = intent_solts[0]
        print("解析后的intent_solts: ", solts)
        tts_text = ""

        # 打开系统网页
        if intent_action == "open_system":
            # 使用webbrowser.open，会阻塞程序，导致AIUI发送2次NLP回调，打开2次浏览器页面，需要在调用前断开信号
            webbrowser.open('http://101.37.148.192:8080?socket=1')

            # 使用以下方式打开，也可以避免打开2次web页面问题
            # import os
            # os.system('start http://101.37.148.192:8080/')
        # 返回主页面
        elif intent_action == "main_page":
            message = '{"pageName":"map","type":"page_change"}'
            self.send_message_to_clients(message)
        # 查看系统模块
        elif intent_action == "open_modules":
            print(solts)
            if solts['slot_value'] == "今日线下门户流量":
                pageName = "offline"
            elif solts['slot_value'] == "平均用户类别排布":
                pageName = "userIdentityCategory"
            elif solts['slot_value'] == "用户数据状态":
                pageName = "userSitua"
            elif solts['slot_value'] == "关联数据类别分布":
                pageName = "browseCategories"
            elif solts['slot_value'] == "今日流量态势":
                pageName = "trafficSitua"
            elif solts['slot_value'] == "主界面":
                pageName = "map"

            message = '{"pageName":"' + pageName + '","type":"page_change"}'
            print(message)

            self.send_message_to_clients(message)

        # 查询在线/离线用户数
        elif intent_action == "query_online_user":
            message = '{"type":"user_query","userDialog":{"date":"' + solts['time'] + '","status":"' + solts[
                'onlineState'] + '","number":"87"}}'
            print(message)

            self.send_message_to_clients(message)

        # 查询今日服务次数，并播报
        elif intent_action == "query_server_times":
            tts_text = '今日服务次数53461次 服务质量90%'
        else:
            print("无效意图")
            tts_text = "我没有理解您的话"

        print('handler end')
        return tts_text
