// 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 XiangCunBianXingJi extends WordGameBaseComponent {

    @property(cc.Node)
    qipaoNan:cc.Node = null;  
    @property(cc.Node)
    qipaoNv:cc.Node = null;  
    @property(cc.Node)
    qipaoNan2:cc.Node = null;  
    @property(cc.Node)
    qipaoNv2:cc.Node = null;  
    @property(cc.Node)
    qipaoMama:cc.Node = null;  
    @property(cc.Node)
    qipaoYeye:cc.Node = null;  

    @property(cc.Node)
    gameEndNode:cc.Node = null;  

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


    finishNvIndex = 0
    FinishNvMax = 4
    
    titleArrayConfig: string[] = [];
    isGameEnd: boolean = false;
    isGuiXia = false

    isTishui = false
    isPiChai = false
    isDushu = false
    isChiMian = false
    start () {
        super.start();
        DaDianScript.userEnterDaDian();

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

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

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode,data_);
        }, this);
        
        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode,data_);
        }, this);
        
        this.showQiPao(this.qipaoNan, '赶紧换回来,这破地方本少爷一秒都呆不下去',3)

    }
    

    normalTouchCheckCallback(targetNode,data_){
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        if(targetNode.name == '新书包'){
        
            this.unlockLevel(0)
            this.finishNvIndex++
       
            this.moveToscree(this.nvHaiNode, false, ()=>{
                this.showQiPao(this.qipaoNv, '城里的学校好壮观,我要更加努力学习考上大学',4, ()=>{
                    if(this.finishNvIndex == this.FinishNvMax){
                        this.showQiPao(this.qipaoMama, '我很想要一个女儿,你又这么懂事,以后你就做我们的干女儿好不好', 4.5)
                        this.node.getChildByName('checkDown').active = true 
                    }
                })
            })
        }else if(targetNode.name == '机票'){
            this.unlockLevel(1)
            this.finishNvIndex++
            this.showQiPao(this.qipaoNv, '外面的世界好大,我要努力读书去世界每个角落都去看看',4.5, ()=>{
                if(this.finishNvIndex == this.FinishNvMax){
                    this.showQiPao(this.qipaoMama, '我很想要一个女儿,你又这么懂事,以后你就做我们的干女儿好不好', 4.5) 
                    this.node.getChildByName('checkDown').active = true
                }
            })
        }else if(targetNode.name == '裙拖动'){
            this.unlockLevel(3)
            this.finishNvIndex++
            this.showQiPao(this.qipaoNv, '城里人真好,有这么多漂亮衣服',3, ()=>{
                if(this.finishNvIndex == this.FinishNvMax){
                    this.showQiPao(this.qipaoMama, '我很想要一个女儿,你又这么懂事,以后你就做我们的干女儿好不好', 4.5) 
                    this.node.getChildByName('checkDown').active = true
                }
            })
        }else if(targetNode.name == '菜2'){
            this.unlockLevel(2)
            this.finishNvIndex++
            this.showQiPao(this.qipaoNv, '城里人的午饭都好丰富',2, ()=>{
                if(this.finishNvIndex == this.FinishNvMax){
                    this.showQiPao(this.qipaoMama, '我很想要一个女儿,你又这么懂事,以后你就做我们的干女儿好不好', 4.5) 
                    this.node.getChildByName('checkDown').active = true
                }
            })
        }else if(targetNode.name == 'checkDown'){
            this.unlockLevel(4)
            this.isGuiXia = true
            this.node.getChildByName('跪2').active = true
            this.showQiPao(this.qipaoNv, '我愿意',2) 
        }else if(targetNode.name == '一袋土豆'){

            this.scheduleOnce(()=>{
                this.moveToscree(this.yeyeNode, true, ()=>{
                    this.yeyeNode.getChildByName('爷爷').getChildByName('土豆袋').active = false
                    this.yeyeNode.getChildByName('爷爷').getChildByName('入学通知书').active = true
                    this.showQiPao(this.qipaoYeye, '家里唯一的食物给你交了学费,你要好好读书')
                })
            },1.0)
     

        }else if(targetNode.name == '木桶'){
            this.unlockLevel(7)
            this.isTishui = true
            this.scheduleOnce(()=>{
                this.moveToscree(this.nanhaiNode, true, ()=>{
                    this.nanhaiNode.getChildByName('男生').getChildByName('木桶有水').getChildByName('空桶').active = false
                    this.showQiPao(this.qipaoNan, '自己劳动挑的山泉水可比买的矿泉水甜多了',3, ()=>{
                        this.nanhaiNode.getChildByName('男生').getChildByName('木桶有水').active = false
                    })
                })
            },1.0)
    
        }else if(targetNode.name == '柴刀'){
            this.unlockLevel(5)
            this.isPiChai = true
            this.scheduleOnce(()=>{
                this.nanhaiNode.getChildByName('柴刀手上').active = false
                this.moveToscree(this.nanhaiNode, true, ()=>{
                    this.nanhaiNode.getChildByName('柴火').active = true
                    this.nanhaiNode.getChildByName('男生').getChildByName('柴火前').active = true
                    this.showQiPao(this.qipaoNan, '原来种地这么幸苦,以后再也不浪费粮食了',3,()=>{
                        this.nanhaiNode.getChildByName('柴火').active = false
                        this.nanhaiNode.getChildByName('男生').getChildByName('柴火前').active = false
                    })
                })
            },1.0)
    

        }else if(targetNode.name == '锅'){
            this.unlockLevel(8)
            if(this.isTishui && this.isPiChai && this.isDushu){
                this.isChiMian = true
                this.nanhaiNode.active = false
                this.node.getChildByName('狂吃spine').active = true
                this.showQiPao(this.qipaoNan, '哎呀妈,真香',3, ()=>{
                    this.nanhaiNode.active = true
                    this.node.getChildByName('狂吃spine').active = false
                    // if(this.finishNvIndex == this.FinishNvMax && this.isGuiXia){
                    //     this.showEndGame()
                    // }
                })
            }else{
                targetNode.active = true
                this.showQiPao(this.qipaoNan, '我王敬泽就算饿死,也不会吃你们一点东西')
            }
        }else if(targetNode.name == '入学通知书'){
            this.unlockLevel(6)
            this.isDushu = true
            this.scheduleOnce(()=>{
                this.nanhaiNode.getChildByName('入学通知书').active = false

                this.moveToscree(this.nanhaiNode, true, ()=>{
                    this.showQiPao(this.qipaoNan, '原来山区的孩子想上学都这么困难')
                })
            },1.0)
         
        }else if(targetNode.name == 'NodeNan'){
            this.unlockLevel(9)
            this.nanhaiNode['recoveposi'] = null
            this.nvHaiNode['recoveposi'] = null
            this.nanhaiNode.active = true
            this.nvHaiNode.active = true
            this.node.getChildByName('跪2').active = false
            this.nanhaiNode.setPosition(cc.v2(this.node.getChildByName('NodeNanPosi').getPosition().x, this.node.getChildByName('NodeNanPosi').getPosition().y))
            this.nvHaiNode.setPosition(cc.v2(this.node.getChildByName('NodeNvPosi').getPosition().x, this.node.getChildByName('NodeNvPosi').getPosition().y))
            this.scheduleOnce(()=>{
             
                this.showEndGame()
            },1.0)
        }else if(targetNode.name == 'NodeNv'){
            this.nanhaiNode['recoveposi'] = null
            this.nvHaiNode['recoveposi'] = null
            this.nanhaiNode.active = true
            this.nvHaiNode.active = true
            this.node.getChildByName('跪2').active = false
            this.nanhaiNode.setPosition(cc.v2(this.node.getChildByName('NodeNanPosi').getPosition().x, this.node.getChildByName('NodeNanPosi').getPosition().y))
            this.nvHaiNode.setPosition(cc.v2(this.node.getChildByName('NodeNvPosi').getPosition().x, this.node.getChildByName('NodeNvPosi').getPosition().y))
            this.scheduleOnce(()=>{
                this.showEndGame()
            },2.0)
        }

    }

    moveToscree(node:cc.Node, isNan,callfunc=null){
        let add = -1700
        if(isNan){
            add = 1700
        }
        let recovePosi = node.getPosition()
        cc.tween(node)
        .to(0.3, {x:recovePosi.x+add, y:recovePosi.y})
        .delay(1.5)
        .to(0.3, {x:recovePosi.x, y:recovePosi.y})
        .call(()=>{
            if(callfunc){
                callfunc()
            }
        })
        .start()
    }

    initParameters(){
        this.titleArrayConfig = this.jsonData.titleArray;

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

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
    }



    showQiPao(qipaoNode, str, delayTime=3, callFunc=null, isPlayEffect = true){
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("str").getComponent(cc.Label).string = str
        if (str && isPlayEffect) {
            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(isSuccess, delay = 3) {
        if( this.isGameEnd ){
            return
        }
        this.isGameEnd = true

        if(isSuccess){
            Game.ins.stopTime();
            this.scheduleOnce(()=>{
                Game.ins.showSuccess()
            },delay)
        }else{
            Game.ins.stopTime();
            this.scheduleOnce(()=>{
                Game.ins.showFail()
            },delay)
        }
     
    }

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

    showEndGame(){
        let endGame = this.node.getChildByName('endGame')
        endGame.zIndex = 99
        this.qipaoNan2.zIndex = 199
        this.qipaoNv2.zIndex = 199
        endGame.active = true
        this.scheduleOnce(()=>{
           
       
            endGame.getChildByName('blackNode').active = false
    
    
            
            let isSuccess = false
            let nanNode = endGame.getChildByName('nan')
            let nvNode = endGame.getChildByName('nv')
            let nanStr = ''
            let nvStr = ''
            let delayNan = 3
            let delayNv = 3
            if(this.isChiMian){
                nanNode.getChildByName('办公司').active = true
                nanNode.getChildByName('success').active = true
                nanNode.getChildByName('fail').active = false
                nanNode.getChildByName('墙壁bg').active = false
                nanStr = '我获得了爸妈的支持,在自己喜欢的领域开创了属于自己的一片天地'
             
                delayNan = 4.5
            }else{
                nanNode.getChildByName('办公司').active = false
                nanNode.getChildByName('success').active = false
                nanNode.getChildByName('fail').active = true
                nanNode.getChildByName('墙壁bg').active = true
                nanStr = '家产都被我全部败光了'
            
                delayNan = 1.5
            }
    
     
    
    
            if(this.finishNvIndex == this.FinishNvMax && this.isGuiXia){
                nvNode.getChildByName('左边bg好').active = true
                nvNode.getChildByName('success').active = true
                nvNode.getChildByName('fail').active = false
                nvNode.getChildByName('左边bg破').active = false
          
                nvStr = '干妈资助我读完了大学,接下来我要努力工作回报社会'
                delayNv = 4
            }else{
                nvNode.getChildByName('左边bg好').active = false
                nvNode.getChildByName('success').active = false
                nvNode.getChildByName('fail').active = true
                nvNode.getChildByName('左边bg破').active = true
           
                nvStr = '这破家太穷了,我要回城里妈妈家去'
                delayNv =2.5
            }
    
                   
            this.showQiPao(this.qipaoNan2, nanStr, delayNan, ()=>{
                this.showQiPao(this.qipaoNv2, nvStr, delayNv, ()=>{
                    if(this.isChiMian && this.finishNvIndex == this.FinishNvMax && this.isGuiXia){
                        isSuccess = true
                    }
                    this.endGameView(isSuccess, 2)
                })
            })
        },2)

   
    }
    // update (dt) {}
}