import Common5 from "../../Platform/th/Common5";
import User from "../../FrameWork/User/User";
import TaskManager from "./TaskManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ChatManager from "./ChatManager";
import EventMgr from "../../FrameWork/Event/EventMgr";
import { ryw_Event } from "../../FrameWork/Event/EventEnum";


export default class JuQingManager{
    //打开对话或微信聊天
    public static openJuQingOrChat(tag,parentNode?){
        let indexStr = this.getCurJuQingUnLockIndex()
        let personConfig = this.getPersonJsonByIndexStr(indexStr)
        // if(personConfig && tag == personConfig.type){
        if(tag == 'WX'){
            PrefabManage.loadPrefabByType(GameType.ChatList,null,(prefabNode)=>{
                prefabNode.getComponent('ChatList').initView('WX')
            })
        }else if(tag == 'DH'){
            let curJuQingUnLockIndex = JuQingManager.getCurJuQingUnLockIndex()
            let juqing2Array = ['DH_88', 'DH_7', 'DH_10', 'DH_11', 'DH_14', 'DH_15', 'DH_16']
            if(juqing2Array.indexOf(curJuQingUnLockIndex)>=0){
                PrefabManage.loadPrefabByType(GameType.JuQingDialog2, null, (prefab)=>{
                    prefab.zIndex = 99
                    if(curJuQingUnLockIndex == 'DH_88'){
                        prefab.getComponent('JuQingDialog').setMengbanOpacity(0)
                    }
                })
            
            }else{
                PrefabManage.loadPrefabByType(GameType.JuQingDialog,parentNode)
            
            }
          
        }else if(tag == 'CX'){
            PrefabManage.loadPrefabByType(GameType.ChatList,null,(prefabNode)=>{
                prefabNode.getComponent('ChatList').initView('CX')
            })
        }
        // }else{
        //     PrefabManage.showTextTips('打开错误')
        // }
    }
    //根据剧情索引str获取jsonConfig
    public static getJuQingJsonByIndexStr(indexStr){
        console.log('Common5.allJuQingChatConfig', Common5.allJuQingChatConfig); 

        for(let i=0;i<Common5.chatPersonConfig.length;i++){
            let eventIndexStr = Common5.chatPersonConfig[i].eventIndexStr
            if(eventIndexStr){
                for(let j=0;j<eventIndexStr.length;j++){
                    if(indexStr == eventIndexStr[j]){
                        let jsonName = Common5.chatPersonConfig[i].jsonName
                        return Common5.allJuQingChatConfig[jsonName]
                    }
                }
            }
            
        }
        return null
    }
    //根据剧情索引str获取personConfig
    public static getPersonJsonByIndexStr(indexStr){
        for(let i=0;i<Common5.chatPersonConfig.length;i++){
            let eventIndexStr = Common5.chatPersonConfig[i].eventIndexStr
            if(eventIndexStr){
                for(let j=0;j<eventIndexStr.length;j++){
                    if(indexStr == eventIndexStr[j]){
                        return Common5.chatPersonConfig[i]
                    }
                }
            }
            
        }
        return null
    }
    //根据jsonName获取personConfig
    public static getPersonJsonByJsonName(jsonName){
        for(let i=0;i<Common5.chatPersonConfig.length;i++){
            if(Common5.chatPersonConfig[i].jsonName == jsonName){
                return Common5.chatPersonConfig[i]
            }
        }
        return null
    }
    //设置当前剧情索引
    public static setCurJuQingUnLockIndex(indexStr){
        User.setCurJuQingUnLockIndex(indexStr)
    }
    //获取当前剧情索引
    public static getCurJuQingUnLockIndex(){
        return User.getCurJuQingUnLockIndex()
    }
    //完成当前剧情
    public static finishCurJuQing(curJuQingUnLockIndex,unLockJuQingIndex){
        this.unLockNewJuQing(unLockJuQingIndex,true)
        TaskManager.juQingFinishEventEmit(curJuQingUnLockIndex)
        EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua, curJuQingUnLockIndex);
    }
    //解锁新剧情
    public static unLockNewJuQing(unLockJuQingIndex,isRefresh?){
        if(!unLockJuQingIndex){
            unLockJuQingIndex = ''
        }else{
            let personConfig = this.getPersonJsonByIndexStr(unLockJuQingIndex)
            if(personConfig){
                ChatManager.setSingleChatStatus(personConfig.type,personConfig.jsonName,unLockJuQingIndex,false)
            }
            
        }
        this.setCurJuQingUnLockIndex(unLockJuQingIndex)
        if(!isRefresh){
            EventMgr.emitEvent_custom(ryw_Event.RefreshJuQingDuiHua);
        }
        
    }
}