import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common5 from "../../../Platform/th/Common5";
import JuQingManager from "../../JuQingChat/JuQingManager";
import TaskManager, { MainTaskIdEnum } from "../../JuQingChat/TaskManager";
import JuQingQiPao from "../../JuQingGuanQia/JuQingQiPao";
import BagManager from "../../Manager/BagManager";
import UserManager from "../../Manager/UserManager";
import TaskResult from "../../MyRoom/TaskResult";
import PrefabManage, { GameType } from "../../PrefabManager/PrefabManage";
import GetAward from "../../SCommon/GetAward";

let ChadianConfig = {
    chadianLog1: [
        {
          str: "废物,我的东西收拾好了没有",
          qiPaoPos: 0,
          delay:3,
          effectUrl: "sound/废物,我的东西收拾好了没有"
        }
    ],
    chadianLog2: [
        {
          str: "爸爸,妈妈呢,妈妈答应给我买裙子的",
          qiPaoPos: 0,
          delay:3,
          effectUrl: "sound/爸爸,妈妈呢,妈妈答应给我买裙子的"
        },
        {
            str: "爸爸给你买裙子吧",
            qiPaoPos: -1,
            delay:3,
            effectUrl: "sound/爸爸给你买裙子吧"
        }
    ]
}

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

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCallback(data_.targetNode);
        }, this); 
        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            console.log('进入DirectTouchMoveCheck')
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);
    }
    
    start () {
        this.showChatBtnStatus()
        EventMgr.onEvent_custom(ryw_Event.RefreshJuQingDuiHua,()=>{
            // this.setNaiNaiSpineStatus()
            console.log('myroom收到RefreshJuQingDuiHua===')
            this.showChatBtnStatus()
        },this)
        
    }
    showChatBtnStatus(){

        // let indexStr = JuQingManager.getCurJuQingUnLockIndex()
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        if(mainId == MainTaskIdEnum.MainTask_202){
            this.node.getChildByName('task202Node').active = true
            this.scheduleOnce(()=>{
                cc.tween(this.node.getChildByName('laoPo'))
                    .delay(2)
                    .to(0.3,{x:0})
                .start()
                cc.tween(this.node.getChildByName('fanPai'))
                    .delay(2)
                    .to(0.3,{x:200})
                    .call(()=>{
                        this.showQiPao(ChadianConfig.chadianLog1[0])
                        // this.node.getChildByName('qiPao_fanPai').active = true
                    })
                    .delay(1.5)
                    .call(()=>{
                        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
                        let mainId = mainTaskInfo.Id
                        if(mainId == MainTaskIdEnum.MainTask_202){
                            this.node.getChildByName('startBtnTask202').active = true
                        }
                    })
                .start()
            },1.5) 
        }else if(mainId == MainTaskIdEnum.MainTask_203){
            PrefabManage.showTextTips('完成剧情演绎,3s自动完成')
            this.scheduleOnce(()=>{
                TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_203)
                TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_204)
                JuQingManager.unLockNewJuQing('WX_202')
            },3)
        }else if(mainId == MainTaskIdEnum.MainTask_208){
            if(BagManager.getBagGoodNums(1730) > 0){
                this.node.getChildByName('startBtnTask208').active = false
            }else{
                this.node.getChildByName('startBtnTask208').active = true
            }
        }else if(mainId == MainTaskIdEnum.MainTask_209){
            
            this.showQiPao(ChadianConfig.chadianLog2[0],()=>{
                this.showQiPao(ChadianConfig.chadianLog2[1])   
            })
        }
    }
    
    normalTouchCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case 'directionArea_化妆品':
                
            case 'directionArea_衣服':
                
            case 'directionArea_钱':
                
            case 'directionArea_鞋子':
                this.finishTaskStep++
                cc.tween(targetNode.attrNodeArray[0])
                    .to(0.6, { x:0,y:0,angle:360,scale:0.2})
                    .set({active:false})
                .start();
                break;
            case 'directionArea_砖块':
                this.node.getChildByName('task202Node').getChildByName('砖块').x += 100
                this.node.getChildByName('task202Node').getChildByName('食谱tipNode').active = true
                this.scheduleOnce(()=>{
                    this.node.getChildByName('task202Node').getChildByName('食谱tipNode').active = false
                },2)
                break;
            default:
                break;
        }
        if(this.finishTaskStep == 4){
            this.node.getChildByName('task202Node').getChildByName('砖块').active = true
        }
    }
    normalTouchCallback(targetNode) {
        switch (targetNode.name) {
            case "湘菜食谱":
                let goodArray = [{goodId:1737, goodNum:1}]
                PrefabManage.loadPrefabByType(GameType.GetAward,null,(prefabNode)=>{
                    prefabNode.getComponent(GetAward).initView(goodArray, ()=>{
                        console.log('完成剧情任务202')
                        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_202)
                        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_203)
                    });
                })
                
                break;
            default:
                break;
        }
    }

    onStartTask202(){
        this.node.getChildByName('task202Node').getChildByName('maskNode').active = false
        this.node.getChildByName('startBtnTask202').active = false
        // this.node.getChildByName('qiPao_fanPai').active = false
    }
    onStartTask208(){
        this.node.getChildByName('startBtnTask208').active = false

        let goodArray = [{goodId:1730, goodNum:1}]
        PrefabManage.loadPrefabByType(GameType.GetAward,null,(prefabNode)=>{
            prefabNode.getComponent(GetAward).initView(goodArray);
        })
    }

    //展示气泡
    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)
    }
}