import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common5 from "../../../Platform/th/Common5";
import ChaoCai from "../../GameMethodRes/ChaoCai/ChaoCai";
import TaskManager, { MainTaskIdEnum } from "../../JuQingChat/TaskManager";
import JuQingQiPao from "../../JuQingGuanQia/JuQingQiPao";
import BagManager from "../../Manager/BagManager";
import InterfaceManager from "../../Manager/InterfaceManager";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import GetAward from "../../SCommon/GetAward";
import GameBaseScript from "../GameBaseScript";

let ChadianConfig = {
    chadianLog2301: [
        {
          str: "感谢各位,林氏中餐必将陈氏西餐赶出去!",
          qiPaoPos: -1,
          delay:3.6,
          effectUrl: "GameRoomRes/sound/Room23/感谢各位,林氏中餐必将陈氏西餐赶出去!"
        }
    ]
}

const {ccclass, property} = cc._decorator;
@ccclass
export default class Room23 extends cc.Component {
   
    finishTaskStep = 0
    onLoad () { 
        this.finishTaskStep = 0

        
    }
    
    start () {
        GameBaseScript.preLoadRemoteAudio(ChadianConfig);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this); 
        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);

        this.showChatBtnStatus()
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua,()=>{
            this.showChatBtnStatus()
        },this)
        EventMgr.onEvent_custom(ryw_Event.updateMainTask,()=>{
            this.showChatBtnStatus()
            this.duiHuaInfo()
        },this)
        this.duiHuaInfo()
    }
    duiHuaInfo(){
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if (mainId == MainTaskIdEnum.MainTask_2301) {
            
            // this.node.getChildByName('mainNode').getChildByName('老婆秘书发抖').active = true
            // this.node.getChildByName('mainNode').getChildByName('老板').active = true
            // this.node.getChildByName('mainNode').getChildByName('秘书站').active = false
            // this.showQiPao(ChadianConfig.chadianLog2101[0], () => {
            //     this.showQiPao(ChadianConfig.chadianLog2101[1], () => {
            //         this.node.getChildByName('mainNode').getChildByName('老板').getComponent(sp.Skeleton).setAnimation(0,'老板消失',false)
            //         this.showQiPao(ChadianConfig.chadianLog2101[2], () => {
            //             this.node.getChildByName('mainNode').getChildByName('老婆秘书发抖').active = false
            //             this.node.getChildByName('mainNode').getChildByName('秘书站').active = true
            //             this.showQiPao(ChadianConfig.chadianLog2101[3], () => {
            //                 TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_2101);
            //                 TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_2102);
            //                 InterfaceManager.setDaoyeList('深海探宝1',2)
            //             });
            //         });  
            //     });
            // });  
        }
    }
    showChatBtnStatus(){
        this.node.getChildByName('startBtnTask2301').active = false
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if(mainId == MainTaskIdEnum.MainTask_2301){
            this.node.getChildByName('startBtnTask2301').active = true
            this.node.getChildByName('mainNode').getChildByName('老板').active = true
        }else{
            this.node.getChildByName('mainNode').getChildByName('老板').active = false
            this.node.getChildByName('mainNode').getChildByName('发小').active = true
            this.node.getChildByName('mainNode').getChildByName('货郎').active = true
            this.node.getChildByName('mainNode').getChildByName('投资人').active = true
            this.node.getChildByName('mainNode').getChildByName('桌子秘书师妹').active = true
            this.node.getChildByName('mainNode').getChildByName('老婆').active = true
        }
    }
    
    normalTouchCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case 'xxxx':
                break;
            default:
                break;
        }
        
    }
    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "新闻":
                // this.showQiPao(ChadianConfig.chadianLog2001[2], () => {
                //     this.showQiPao(ChadianConfig.chadianLog2001[3], () => {
                //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_2001);
                //         TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_2002);
                //     });
                // });  
                break;
            default:
                break;
        }
        
        if(this.finishTaskStep == 4){
            // this.node.getChildByName('mainNode').getChildByName('touchArea_菜谱').active = true
        }
    }

    //展示气泡
    showQiPao(curLog, func?) {

        console.log("curLog==", curLog)
        if (!curLog) {
            console.log("xxxxxx")
            return;
        }
        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos


        if (qiPaoPos_ != -1) {
            if (curLog.effectUrl && curLog.effectUrl.length > 0) {
                Common5.playRemoteAudioEffect(curLog.effectUrl);
            }

            let node = this.node
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            this.showDialogStr(string_,qiPao.getChildByName("str"))
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(curLog.delay)
                .call(() => {
                    qiPao.active = false;
                    if (func) {
                        func();
                    }
                })
                .start();
        }
        else {
            let qiPao = this.node.getChildByName("dh_qiPao")
            qiPao.getComponent(JuQingQiPao).initView(curLog, func)
        }
    }
    showDialogStr(str,dialogStr,finishFunc?){
        let curStr = ""
        let curIndex = 0
        let callFunc = ()=>{
            curStr += str[curIndex++]
            dialogStr.getComponent(cc.Label).string = curStr
            if(curIndex >= str.length){
                finishFunc && finishFunc()
            }
        }
        this.schedule( callFunc , 0.07,str.length-1)
    }
    onStartTask2301(){
        let goodArray = [{ goodId: 3098, goodNum: 1 }];
        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(goodArray, () => {
                BagManager.subBagList(3053,1)
                BagManager.subBagList(3054,1)
                BagManager.subBagList(3055,1)
                BagManager.subBagList(3056,1)
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_2301);
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_2302);
                InterfaceManager.setDaoyeList('深海探宝2',3)
                this.showQiPao(ChadianConfig.chadianLog2301[0], () => {
                
                });
            });
        });
    }
}