// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import { ryw_Event } from "../../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../../FrameWork/Event/EventMgr";
import Common from "../../../../FrameWork/Util/Common";
import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PingKuYiJia extends WordGameBaseComponent {

    @property(cc.Node)
    qipaoNode:cc.Node = null;  
    @property(cc.Node)
    qipaoNode2:cc.Node = null;  
    @property(cc.Node)
    qipaoNodeNanwa:cc.Node = null;  
    @property(cc.Node)
    qipaoNodeNvWa:cc.Node = null;  
    @property(cc.Node)
    huliSpine:cc.Node = null;  

    @property(cc.Node)
    nainaiNode:cc.Node = null;  
    @property(cc.Node)
    yeyeNode:cc.Node = null;  
    @property(cc.Node)
    nanhaiNode:cc.Node = null;  
    @property(cc.Node)
    nvhaiNode:cc.Node = null;  


    curIndex = 0
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianArrayConfig: any[] = [];
    wrongAnswerConfig: string[] = [];
    answersDuihuaConfig: any[] = [];
    checkNodeArray: string[] = [];
    // LIFE-CYCLE CALLBACKS:
    curFinishNum:number = 0
    chadianMax = 7
    // onLoad () {}
    chadianMax2 = 10
    isAllRight = true

    start () {
        super.start();
        DaDianScript.userEnterDaDian();

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/PingKuYiJiaConfig',(assest)=>{
            this.jsonData = assest.json
            this.initParameters();
           
        })

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

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);
        
        // EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
        //     this.failView();
        // }, this);
    }

    
    initParameters(){
        this.titleArrayConfig = this.jsonData.titleArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.chadianArrayConfig = this.jsonData.chadianArray;
        this.wrongAnswerConfig = this.jsonData.wrongAnswer;
        this.answersDuihuaConfig = this.jsonData.answersDuihua;
        this.checkNodeArray = this.jsonData.checkNode;

        Common.Type = 3;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.answersArray;

        this.curIndex = 0
  

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
        let qianbiNodes = this.node.getChildByName("纸币堆1")
        this.specialNodeTouchEvent(qianbiNodes)
    }


    normalTouchCheckCallback(targetNode){
        
    }


    showQiPao(qipaoNode, str, delayTime=3, callFunc=null){
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("str").getComponent(cc.Label).string = str
        if (str) {
            Common5.playEffectCustom(Common5.selectGameInfo.bundle, "sound/"+str);
        }
        cc.tween(qipaoNode)
            .to(0.2,{scale:1})
            .delay(delayTime)

            .call(()=>{
                qipaoNode.active = false
                if(callFunc){
                    callFunc()
                }
            })
        .start()
    }

    endGameView(delay = 3) {
        if(this.curIndex>=10){
            Game.ins.stopTime();
            this.scheduleOnce(()=>{
                Game.ins.showSuccess()
            },delay)
        }
     
    }

    unlockLevel(sublevel) {
        Game.ins.tipUnlock(sublevel)
    }


    
    //特殊节点的点击事件(主要处理节点可以移动的问题)
    specialNodeTouchEvent(qianbiNodes){
        for(let i=0;i<qianbiNodes.childrenCount;i++){
            this.openTouchEvent(qianbiNodes.children[i], this.checkNodeArray)
        }
    }


    openTouchEvent(node, checkNodeTab){
        var attrs = {
            isCanMove:true,
            checkNodeTab: checkNodeTab,
            startPos: node.getPosition(),//初始位置
        };
        node.attr(attrs)
        node.on(cc.Node.EventType.TOUCH_START,this.touchStartSpecicalNode,this)
        node.on(cc.Node.EventType.TOUCH_MOVE,this.touchMoveSpecicalNode,this)
        node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchEndSpecicalNode,this)
        node.on(cc.Node.EventType.TOUCH_END,this.touchEndSpecicalNode,this)
    }

    closeTouchEvent(node){
        node.off(cc.Node.EventType.TOUCH_START,this.touchStartSpecicalNode,this)
        node.off(cc.Node.EventType.TOUCH_MOVE,this.touchMoveSpecicalNode,this)
        node.off(cc.Node.EventType.TOUCH_CANCEL,this.touchEndSpecicalNode,this)
        node.off(cc.Node.EventType.TOUCH_END,this.touchEndSpecicalNode,this)
    }

    touchStartSpecicalNode(event){
        Common5.playEffect("click")
       
    }
    touchMoveSpecicalNode(event){
        
        let target = event.target;
        if(!target.isCanMove){
            return
        }
        let posi = event.getLocation()//世界坐标
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)
    }

    saqianAnim(){
        let qianbiNodes = this.node.getChildByName("纸币堆2")
        qianbiNodes.active = true

        let zhibiNode = this.node.getChildByName('纸币')
        for(let i = 0; i<qianbiNodes.childrenCount;i++){

            let copyNode = cc.instantiate(zhibiNode)
            copyNode.parent = zhibiNode.parent
            copyNode.active = true
            let posiArray = Common5.getNodeToTargetPos(copyNode, qianbiNodes.children[i])
            copyNode.scale = 0.1
            cc.tween(copyNode)
            .delay(0.2*i)
            .to(1.0, {x:posiArray.x, y:posiArray.y, scale:1})
            .call(()=>{
                qianbiNodes.children[i].active = true
                if(i==qianbiNodes.childrenCount-1){
                    let qianbiNodes = this.node.getChildByName("纸币堆2")
                    this.specialNodeTouchEvent(qianbiNodes)
                   
                }
               
                
                copyNode.removeFromParent()
                copyNode.destroy()
                
            })
            .start()


        }

  
    }


    touchEndSpecicalNode(event){
        // event.target.zIndex = 0 
        
        let target = event.target;
        let checkTarget = this.checkArea(target,target.checkNodeTab)
        if (checkTarget) {
            this.curFinishNum ++
            console.log(checkTarget.name, 'checkTarget.name+++++====')
            target.active = false
            checkTarget.active = false
            if(checkTarget.name == 'check狐狸1'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸站立',true)
                this.showQiPao( this.qipaoNode, '谢谢爷爷,我还有点冷', 3)
                this.unlockLevel(0)
            }else if(checkTarget.name == 'check狐狸2'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸穿衣',true)
                this.showQiPao( this.qipaoNode, '谢谢爷爷,我还缺点首饰', 3)
                this.unlockLevel(1)
            }else if(checkTarget.name == 'check狐狸3'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸戴项链',true)
                this.showQiPao( this.qipaoNode, '谢谢爷爷,我还想要一副墨镜', 3)
                this.unlockLevel(2)
            }else if(checkTarget.name == 'check狐狸4'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸戴墨镜',true)
                this.showQiPao( this.qipaoNode, '谢谢爷爷,我还想要一双华伦天奴', 3)
                this.unlockLevel(3)
            }else if(checkTarget.name == 'check狐狸5'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸高跟鞋',true)
                this.showQiPao( this.qipaoNode, '谢谢爷爷,再给我买辆车', 3)
                this.unlockLevel(4)
            }else if(checkTarget.name == 'check狐狸6'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸车钥匙',true)
                this.showQiPao( this.qipaoNode, '谢谢爷爷,再给一点零花钱', 3)
                this.unlockLevel(5)
            }else if(checkTarget.name == 'check狐狸7'){
                this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸零花钱',true)
                this.unlockLevel(6)
                this.showQiPao( this.qipaoNode, '爷爷等我回来报答你', 3, ()=>{
                    this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'狐狸出门',false)
                    this.scheduleOnce(()=>{
                        this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'双人进门',false)

                        this.scheduleOnce(()=>{
                            this.huliSpine.getComponent(sp.Skeleton).setAnimation(0,'撒钱',true)
                            //对话
                            Common5.playEffectCustom(Common5.selectGameInfo.bundle, "sound/撒钱");
                            this.showQiPao( this.qipaoNode2, '这是我娶小美的彩礼,以后小美就交给我了', 4, ()=>{
                                this.huliSpine.active = false
                            })

                            this.scheduleOnce(()=>{
                                this.saqianAnim()
                            },1.0)
                         

                        },1.3)
                    

                    },2.0)
                })

            }else {
                this.isAllRight = false
                if(this.curIndex == 0){
                    Common.SpeTip = '您已触发错误操作,请重试'
                }
                if(checkTarget.name == 'check门'){
                    this.node.getChildByName('门旧').active = false
                    this.node.getChildByName('门新').active = true
                    if(this.curIndex == 1){
                        this.unlockLevel(14)
                    }
                }else if(checkTarget.name == 'check窗户'){
                    this.node.getChildByName('旧窗户').active = false
                    this.node.getChildByName('新窗户').active = true
                    if(this.curIndex == 1){
                        this.unlockLevel(13)
                    }
                }else if(checkTarget.name == 'check床'){
                    this.node.getChildByName('草席').active = false
                    this.node.getChildByName('床铺').active = true
                    if(this.curIndex == 1){
                        this.unlockLevel(10)
                    }
                }else if(checkTarget.name == 'check母鸡'){
                    this.node.getChildByName('鸡').active = false
                    this.node.getChildByName('烤鸡').active = true
                    if(this.curIndex == 1){
                        this.unlockLevel(15)
                    }
                }else if(checkTarget.name == 'check奶奶'){
                    if(this.curIndex == 1){
                        this.unlockLevel(11)
                    }
                    this.nainaiNode.getComponent(sp.Skeleton).setAnimation(0,'老奶奶2',true)

                }else if(checkTarget.name == 'check爷爷'){
                    this.yeyeNode.getComponent(sp.Skeleton).setAnimation(0,'老爷爷2',true)
                    if(this.curIndex == 1){
                        this.unlockLevel(9)
                    }
                }else if(checkTarget.name == 'check男娃1'){
                    this.nanhaiNode.getComponent(sp.Skeleton).setAnimation(0,'小男孩新衣服',true)
                    
                    if(this.curIndex == 0){
                        this.showQiPao( this.qipaoNodeNanwa, '爷爷,我想穿新衣服', 3)
                    }else{
                        this.unlockLevel(8)
                    }
                  
                }else if(checkTarget.name == 'check男娃2'){
                    this.nanhaiNode.getComponent(sp.Skeleton).setAnimation(0,'小男孩棉袄',true)
                    this.showQiPao( this.qipaoNodeNanwa, '爷爷,我想打扮的漂亮一点', 3)
                  
                }else if(checkTarget.name == 'check男娃3'){
                    this.nanhaiNode.getComponent(sp.Skeleton).setAnimation(0,'小男孩杀马特',true)
                   
                    this.showQiPao( this.qipaoNodeNanwa, '爷爷我兄弟来找我了,我出去了', 3, ()=>{
                        this.nanhaiNode.getComponent(sp.Skeleton).setAnimation(0,'小男孩出门',false)
                    })

                }else if(checkTarget.name == 'check女娃1'){
                    this.nvhaiNode.getComponent(sp.Skeleton).setAnimation(0,'女孩新衣服',true)
                   
                    if(this.curIndex == 0){
                        this.showQiPao( this.qipaoNodeNvWa, '爷爷,我想穿新衣服。', 3)
                    }else{
                        this.unlockLevel(7)
                    }
                }else if(checkTarget.name == 'check女娃2'){
                    this.nvhaiNode.getComponent(sp.Skeleton).setAnimation(0,'女孩棉袄',true)
                    this.showQiPao( this.qipaoNodeNvWa, '爷爷,我想打扮的漂亮一点。', 3)
                }else if(checkTarget.name == 'check女娃3'){
                    this.nvhaiNode.getComponent(sp.Skeleton).setAnimation(0,'女孩杀马特',true)
                    this.showQiPao( this.qipaoNodeNvWa, '我闺蜜来找我了,我出去了', 3, ()=>{
                        this.nvhaiNode.getComponent(sp.Skeleton).setAnimation(0,'女孩出门',false)
                    })
      
                }else if(checkTarget.name == 'check墙壁'){
                    this.node.getChildByName('破旧背景').active = false
                    this.node.getChildByName('新背景').active = true
                    if(this.curIndex == 1){
                        this.unlockLevel(16)
                    }
                }else if(checkTarget.name == 'check火柴堆'){
                    this.node.getChildByName('火柴堆').getComponent(sp.Skeleton).setAnimation(0,'小太阳',true)
                    if(this.curIndex == 1){
                        this.unlockLevel(12)
                    }
                }
            }
            this.checkIsFinish()
        }else{
            event.target.setPosition(event.target.startPos)
        }
    }


    checkIsFinish(){
        if(this.curIndex == 0){
            if(this.curFinishNum >= this.chadianMax){
                this.curIndex++
                this.curFinishNum = 0
                //下一个阶段
                if(this.isAllRight){
                    this.node.getChildByName('check男娃2').active = false
                    this.node.getChildByName('check男娃3').active = false
                    this.node.getChildByName('check女娃2').active = false
                    this.node.getChildByName('check女娃3').active = false

                    // let qianbiNodes = this.node.getChildByName("纸币堆2")
                    // this.specialNodeTouchEvent(qianbiNodes)

                }else{
                    this.scheduleOnce(()=>{
                        Game.ins.showFail()
                    },1)  
                }
            }
        }else{
            if(this.curFinishNum >= this.chadianMax2){
                console.log('结算+++===')
                this.scheduleOnce(()=>{
                    if(this.curFinishNum >= this.chadianMax){
                        Game.ins.showSuccess()
                    }
                },2)
            }
        }
   
        
    }


    checkArea(node,checkNodeTab){
        let checkIndex = null
        for(let i=0;i<checkNodeTab.length;i++){
            let target = this.node.getChildByName(checkNodeTab[i])

            if(target && target.active && Common5.checkContainsNode(target,node)){
                checkIndex = target
                break
            }


        }
        return checkIndex
    }


    // update (dt) {}
}