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

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameStory2 extends cc.Component {
    // isStartTask603:boolean = false
    taskGoodStep:number = 0

    onLoad () {
        
    }

    start () {
        EventMgr.onEvent_custom(ryw_Event.goodTouchMoveCheck, (targetNode) => {
            this.checkTaskGoodStep(targetNode);
        }, this);
        this.checkTask207()
        // this.checkTask603()
    }
    checkTask207(){
        // let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        // let mainId = mainTaskInfo.Id
        // if(mainId == MainTaskIdEnum.MainTask_602){
        //     this.node.getChildByName('标题框').active = false
        //     this.node.getChildByName('juQingTipNode').active = false
        //     PrefabManage.showTextTips('前往售货机购买物品工具')
        //     // this.node.getChildByName('jieShengTask504Node').active = true
        //     // this.node.getChildByName('mainNode').getChildByName('床边女人').getComponent(sp.Skeleton).setAnimation(0,'怀孕2',true)

        //     let isHave1701 = (BagManager.getBagGoodNums(1701) > 0)
        //     let isHave1702 = (BagManager.getBagGoodNums(1702) > 0)
        //     let isHave1703 = (BagManager.getBagGoodNums(1703) > 0)
        //     let isHave1704 = (BagManager.getBagGoodNums(1704) > 0)
        //     let nodex = this.node.getChildByName('nodex')
        //     nodex.getChildByName('icon_1701').getChildByName('spr1').active = isHave1701
        //     nodex.getChildByName('icon_1701').getChildByName('spr2').active = !isHave1701
        //     nodex.getChildByName('icon_1702').getChildByName('spr1').active = isHave1702
        //     nodex.getChildByName('icon_1702').getChildByName('spr2').active = !isHave1702
        //     nodex.getChildByName('icon_1703').getChildByName('spr1').active = isHave1703
        //     nodex.getChildByName('icon_1703').getChildByName('spr2').active = !isHave1703
        //     nodex.getChildByName('icon_1704').getChildByName('spr1').active = isHave1704
        //     nodex.getChildByName('icon_1704').getChildByName('spr2').active = !isHave1704
        //     if(isHave1701 && isHave1702 && isHave1703 && isHave1704){
        //         // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_504)
        //         // let day = UserManager.getCurDay()
        //         // let taskInfo:MainTaskDataInfo = {
        //         //     Id:MainTaskIdEnum.MainTask_505,
        //         //     StartDay:day,
        //         //     EndDay:day+10000,
        //         //     IsFinish:false,
        //         // }
        //         // TaskManager.setCurUnLockMainTaskInfo(taskInfo)
        //         // console.log('解锁主任务505',taskInfo)
        //         this.node.getChildByName('startBtnTask603').active = true
        //     }
        // }
    }
    // onBtnStartTask603(){
    //     let isHave1701 = (BagManager.getBagGoodNums(1701) > 0)
    //     let isHave1702 = (BagManager.getBagGoodNums(1702) > 0)
    //     let isHave1703 = (BagManager.getBagGoodNums(1703) > 0)
    //     let isHave1704 = (BagManager.getBagGoodNums(1704) > 0)
    //     if(isHave1701 && isHave1702 && isHave1703 && isHave1704){
    //         // User.setIsStartTask505(true)
    //         // this.isStartTask603 = true
    //         this.checkTask603()
            
    //     }else{
    //         PrefabManage.showTextTips('请先去售货机购买所需物品')
    //     }
        
    // }
    // checkTask603(){
    //     let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
    //     let mainId = mainTaskInfo.Id
    //     let isBtnShow = true
    //     if(mainId == MainTaskIdEnum.MainTask_603){
    //         this.node.getChildByName('背景2').active = true
    //         PrefabManage.showTextTips('拖动物品完成洗浴')
    //         // this.node.getChildByName('jieShengTask504Node').getChildByName('guideLabel').active = true
    //         // this.node.getChildByName('jieShengTask504Node').active = true
    //         // this.node.getChildByName('mainNode').getChildByName('床边女人').getComponent(sp.Skeleton).setAnimation(0,'怀孕2',true)

    //         // if(this.isStartTask603){
    //         //     // Common5.playMusicCustom('MainPrefab', 'MyRoom/sound/接生关卡');
    //         //     isBtnShow = false

                
    //         //     this.node.getChildByName('juQingTipNode').active = true
    //         //     this.node.getChildByName('maskNode').active = false
    //         //     this.node.getChildByName('标题框').active = true
    //         //     this.node.getChildByName('startBtnTask603').active = false
    //         //     // this.node.getChildByName('jieShengTask504Node').getChildByName('手指箭头').active = true
    //         // }else{
    //         //     this.node.getChildByName('startBtnTask603').active = true
    //         // }
    //     }else{
    //         this.node.getChildByName('背景2').active = false
    //     }
    //     EventMgr.emitEvent_custom(ryw_Event.SetCommonBtnShow,isBtnShow);
    //     // this.setCommonBtnShow(isBtnShow)
    // }
    checkTaskGoodStep(targetNode){
        let rightStepTab = ['icon_3031','icon_3032','icon_3033']
        
        let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        let callFunc = (typeStr)=>{
            if(typeStr == 0){
                let spineNode = this.node.getChildByName('治疗Spine')
                spineNode.stopAllActions()
                
                if(targetNode.parent.name == 'icon_3031'){
                    cc.tween(spineNode)
                        .call(()=>{
                            spineNode.getComponent(sp.Skeleton).setAnimation(0,'玻璃渣',false)
                        })
                    .start()
                    this.node.getChildByName('手指箭头').active = false;
                }else if(targetNode.parent.name == 'icon_3032'){
                    cc.tween(spineNode)
                        .call(()=>{
                            spineNode.getComponent(sp.Skeleton).setAnimation(0,'碘酒',false)
                        })
                    .start()
                }else if(targetNode.parent.name == 'icon_3034'){
                    cc.tween(spineNode)
                        .call(()=>{
                            spineNode.getComponent(sp.Skeleton).setAnimation(0,'药膏',false)
                        })
                    .start()
                }else if(targetNode.parent.name == 'icon_3033'){
                    cc.tween(spineNode)
                        .call(()=>{
                            spineNode.getComponent(sp.Skeleton).setAnimation(0,'创可贴',false)
                        })
                    .start()
                }
            }else if(typeStr == 1){
                let wrongTip = this.node.getChildByName('wrongTip')
                wrongTip.stopAllActions()
                cc.tween(wrongTip)
                    .set({active:true,scale:0})
                    .to(0.2,{scale:1})
                    .delay(2)
                    .to(0.2,{scale:0})
                    .set({active:false})
                .start()
            }
        }
        //结束操作后弹框成功或失败
        if(targetNode.parent.name == rightStepTab[this.taskGoodStep]){
            console.log('物品正确',targetNode.parent.name)
            // this.node.getChildByName('jieShengTask504Node').getChildByName('手指箭头').active = false
            this.taskGoodStep++
            if(this.taskGoodStep >= rightStepTab.length){
                
                let successFunc = ()=>{
                    this.finishTask603()
                    
                }
                cc.tween(this.node)
                    .delay(2.5)
                    .call(()=>{
                        PrefabManage.loadPrefabByType(GameType.TaskResult,null,(prefabNode)=>{
                            prefabNode.getComponent(TaskResult).initView1(1,true,successFunc)
                        })
                    })
                .start()
            }
            callFunc(0)
        }else{
            console.log('物品错误',targetNode.parent.name)
            callFunc(1)
        }
        
    }
    finishTask603(){
        // this.isStartTask603 = false
        // User.setRoomGoodStatus(5)
        TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_207)
        TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_208)
        BagManager.subBagList(3031,1)
        BagManager.subBagList(3032,1)
        BagManager.subBagList(3033,1)
        // BagManager.subBagList(3034,1)

        
        Common5.playMusicCustom('CommonRes', 'sound/背景音');
        this.node.removeFromParent()
        this.node.destroy()
        // Common5.releaseBundleAssets('GameStory2')
    }
    onBtnTest(){
        
    }
}