// 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 zhijiaoScript extends WordGameBaseComponent {
    @property(cc.Node)
    layer1:cc.Node = null;  
    @property(cc.Node)
    layer2:cc.Node = null;  
    @property(cc.Node)
    layer3:cc.Node = null;  
    @property(cc.Node)
    leftNode:cc.Node = null;  
    @property(cc.Node)
    rightNode:cc.Node = null;  
    
    @property(cc.Node)
    copyzhibiLeft:cc.Node = null;  
    @property(cc.Node)
    copyzhibiRight:cc.Node = null;  
    
    
    titleArrayConfig: string[] = [];
    isGameEnd = false
    leftzhibiIndex = 0
    zhengqianIndex = 0
    rightzhibiIndex = 0
    isKaiChuang = false
    start () {
        super.start();
        DaDianScript.userEnterDaDian();

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/zhijiaoConfig',(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.layer1.getChildByName('qiPaoNodenan'), '这里有个去偏远山区支教的名额,你们两谁愿意去',3, ()=>{

        })


    }
    
    zhibiAnim(curNode){
        let zhibi1 = curNode.getChildByName('纸币2')
        let qianbiCheck = curNode.getChildByName('zhibi')
        for(let i=0; i<qianbiCheck.childrenCount;i++){
            let copy = cc.instantiate(zhibi1)
            copy.parent = zhibi1.parent
            let posi = Common5.getNodeToTargetPos(copy, qianbiCheck.children[i])
            let node = qianbiCheck.children[i]
            copy.scale = 0.1
            copy.active = true
            cc.tween(copy)
            .to(0.5+0.2*i,{x:posi.x, y:posi.y, scale:1})
            .call(()=>{
                node.active = true
            })
            .removeSelf()
            .start()
        }
    }

    oneZhibiAnim(curNode,qianbiChild:cc.Node){
        let zhibi1 = curNode
        let copy = cc.instantiate(zhibi1)
        copy.parent = zhibi1.parent
        let posi = Common5.getNodeToTargetPos(copy, qianbiChild)
        let node = qianbiChild
        copy.scale = 0.1
        copy.active = true
        cc.tween(copy)
        .to(0.3,{x:posi.x, y:posi.y, scale:1})
        .call(()=>{
            node.active = true
        })
        .removeSelf()
        .start()
    }


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

    opacityAnim(node, call){
        node.active = true
        node.opacity = 0
        cc.tween(node)
        .to(1.0, {opacity:255})
        .call(()=>{
            if(call){
                call()
            }
        })
        .start()
    }


    normalTouchCheckCallback(targetNode,data_){
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        if(targetNode.name == '支教志愿书2'){
             
            if(data_.jihuoNode.name == 'nongcun'){
                this.showQiPao(this.layer1.getChildByName('qiPaonong'), '校长我去吧',1, ()=>{
                    this.opacityAnim(this.layer2, ()=>{
                        this.layer1.active = false 
                        this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '学校下来了一笔补助金',2, ()=>{
                            this.zhibiAnim(this.leftNode)
                            this.showQiPao(this.rightNode.getChildByName('qiPaonong'), '想不到他们的学习条件这么艰苦,我得尽自己的能力去帮助他们',4)
                        })
                    }) 
                })
            }else if(data_.jihuoNode.name == 'chengshi'){
                this.layer1.getChildByName('女1').getChildByName('惊恐表情').active = true
                this.showQiPao(this.layer1.getChildByName('qiPaocheng'), '我妈妈生病了离不开人,我可能去不了',3, ()=>{
                    targetNode.active = true
                    this.layer1.getChildByName('女1').getChildByName('惊恐表情').active = false
                })
            }
        }else if(targetNode.name == 'left纸币1'){
            this.leftQianCheck(targetNode,data_.jihuoNode.name)
        }else if(targetNode.name == 'left纸币2'){
            this.leftQianCheck(targetNode,data_.jihuoNode.name)
        }else if(targetNode.name == 'left纸币3'){
            this.leftQianCheck(targetNode,data_.jihuoNode.name)
        }else if(targetNode.name == 'left纸币4'){
            this.leftQianCheck(targetNode,data_.jihuoNode.name)
        }else if(targetNode.name == 'left纸币5'){
            this.leftQianCheck(targetNode,data_.jihuoNode.name)
        }else if(targetNode.name == '锄头2'){
           
            this.scheduleOnce(()=>{
                
                this.moveToscree(this.rightNode.getChildByName('laoshi'), ()=>{
                    this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币1'))
                    this.showQiPao(this.rightNode.getChildByName('qiPaonong'), '我去帮老乡干活赚的一点零用钱给孩子们',3,()=>{
                        this.zhengqianIndex++
                        if(this.zhengqianIndex == 2){
                            this.showQiPao(this.rightNode.getChildByName('qiPaonong'), '我存的零花钱跟工资都拿出来留给孩子们',3,()=>{
                                this.rightNode.getChildByName('laoshi').getChildByName('待机老师').active = true
                                this.rightNode.getChildByName('laoshi').getChildByName('老师拿扫把劳动').active = false
                                this.rightNode.getChildByName('laoshi').getChildByName('老师拿锄头').active = false
                                this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币3'))
                                this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币4'))
                                this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币5'))
                            })
                        }
                    })
                    
                })
            },0.5)
       
        }else if(targetNode.name == '扫把2'){
            this.scheduleOnce(()=>{
                
                this.moveToscree(this.rightNode.getChildByName('laoshi'), ()=>{
                    this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币2'))
                    this.showQiPao(this.rightNode.getChildByName('qiPaonong'), '我去打扫卫生赚的一点零用钱给孩子们',3,()=>{
                        this.zhengqianIndex++
                        if(this.zhengqianIndex == 2){

                            this.showQiPao(this.rightNode.getChildByName('qiPaonong'), '我存的零花钱跟工资都拿出来留给孩子们',3,()=>{

                                this.rightNode.getChildByName('laoshi').getChildByName('待机老师').active = true
                                this.rightNode.getChildByName('laoshi').getChildByName('老师拿扫把劳动').active = false
                                this.rightNode.getChildByName('laoshi').getChildByName('老师拿锄头').active = false

                                this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币3'))
                                this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币4'))
                                this.oneZhibiAnim(this.copyzhibiRight, this.rightNode.getChildByName('zhibi').getChildByName('纸币5'))
                            })
                        }
                    })
                    
                })
            },0.5)
     
        }else if(targetNode.name == '纸币1'){
            this.rightQianCheck(targetNode,data_.jihuoNode)
        }else if(targetNode.name == '纸币2'){
            this.rightQianCheck(targetNode,data_.jihuoNode)
        }else if(targetNode.name == '纸币3'){
            this.rightQianCheck(targetNode,data_.jihuoNode)
        }else if(targetNode.name == '纸币4'){
            this.rightQianCheck(targetNode,data_.jihuoNode)
        }else if(targetNode.name == '纸币5'){
            this.rightQianCheck(targetNode,data_.jihuoNode)
        }else if(targetNode.name == '捐赠金2'){
            this.showQiPao(this.layer3.getChildByName('qiPaotongxue1'), '老师,感谢您当年对我们的教导,如今我跟社会上一些爱心人事一起来帮助这些孩子们',5)
        }else if(targetNode.name == '人物背面'){
            this.showQiPao(this.layer3.getChildByName('qiPaotongxue2'), '老师,我们记得您的教导,去帮助了一群贫苦山区的学生,这不马上就赶回来了吗',5, ()=>{
                this.endGameView(true,1)
            })
        }
        
        
        

    }

    rightQianCheck(targetNode,jihuoNode){
        let node = this.rightNode.getChildByName(jihuoNode.name)
        if(node['ischeck']){
            targetNode.active = true
            return
        }

        
        node['ischeck'] = true
        if(jihuoNode.name == 'xuecheck'){
            this.rightzhibiIndex++
            this.rightNode.getChildByName('校服学生学生').active = true
            this.rightNode.getChildByName('破烂学生').active = false
            this.showQiPao(this.rightNode.getChildByName('qiPaoxue'), '新校服穿着真舒服',1, ()=>{
                this.zhuanchangAnim()
            })
        }else if(jihuoNode.name == 'kezhuocheck'){
            this.rightzhibiIndex++
            this.rightNode.getChildByName('课桌好').active = true
            this.rightNode.getChildByName('课桌破').active = false
            this.rightNode.getChildByName('讲台好').active = true
            this.rightNode.getChildByName('讲台破').active = false
            this.showQiPao(this.rightNode.getChildByName('qiPaoxue'), '新课桌就不会摇摇晃晃了',1, ()=>{
                this.zhuanchangAnim()
            })
        }else if(jihuoNode.name == 'dengcheck'){
            this.rightzhibiIndex++
            this.rightNode.getChildByName('日光灯').active = true
            this.rightNode.getChildByName('破旧灯泡').active = false
            this.showQiPao(this.rightNode.getChildByName('qiPaoxue'), '晚上上课终于能看的清课本的字了',3, ()=>{
                this.zhuanchangAnim()
            })
        }else if(jihuoNode.name == 'qiangcheck'){
            this.rightzhibiIndex++
            this.rightNode.getChildByName('黑板').active = true
            this.rightNode.getChildByName('正常墙').active = true
            this.rightNode.getChildByName('破旧正常墙').active = false
            this.showQiPao(this.rightNode.getChildByName('qiPaoxue'), '我们也有黑板啦',1, ()=>{
                this.zhuanchangAnim()
            })
        }else if(jihuoNode.name == 'chuancheck'){
            if(!this.isKaiChuang){
                targetNode.active = true
                jihuoNode.active = true
                node['ischeck'] = false
                return
            }
            this.rightzhibiIndex++
            this.rightNode.getChildByName('窗户好').active = true
            this.rightNode.getChildByName('窗户破旧').active = false
            this.showQiPao(this.rightNode.getChildByName('qiPaoxue'), '课余时间我们也能出去运动一下了',3, ()=>{
                this.zhuanchangAnim()
            })

        }
    }

    zhuanchangAnim(){
        if(this.leftzhibiIndex == 4 && this.rightzhibiIndex == 5){
            this.node.getChildByName('layer25').active = true
            this.layer2.active = false
            
            this.scheduleOnce(()=>{
                this.layer3.active = true
                this.node.getChildByName('layer25').active = false
                this.showQiPao(this.layer3.getChildByName('qiPaocheng'), '怎么我的学生一个都没有回来参加聚会')
            },2.0)
        }
  
    }

    leftQianCheck(targetNode,name){
        let node = this.leftNode.getChildByName(name)
        if(node['ischeck']){
            targetNode.active = true
            return
        }

        this.leftzhibiIndex++
        node['ischeck'] = true
        if(name == 'xue1check'){
            this.leftNode.getChildByName('课桌好').active = true
            this.leftNode.getChildByName('讲台好').active = true
            this.leftNode.getChildByName('左边课桌普通').active = false
            this.leftNode.getChildByName('讲台普通').active = false
            this.leftNode.getChildByName('灯好').active = true
            this.leftNode.getChildByName('灯坏').active = false

            this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '先帮你们换批新课桌',2, ()=>{
                if(this.leftzhibiIndex == 4){
                    this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '能够帮助孩子们在良好的环境中学习的更好让我很自豪',4 ,()=>{
                        this.zhuanchangAnim()
                    })
                }            
            })
        }else if(name == 'xue2check'){
            this.leftNode.getChildByName('新文具').active = true
            this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '学习物品也少不了',2, ()=>{
                if(this.leftzhibiIndex == 4){
                    this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '能够帮助孩子们在良好的环境中学习的更好让我很自豪',4 ,()=>{
                        this.zhuanchangAnim()
                    })
                }           
            })
        }else if(name == 'heibancheck'){
            this.leftNode.getChildByName('多媒体').active = true
            this.leftNode.getChildByName('墙壁好').active = true
            this.leftNode.getChildByName('墙壁普通').active = true
            this.leftNode.getChildByName('黑板').active = true
            this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '这样以后上课就更方便了',2, ()=>{
                if(this.leftzhibiIndex == 4){
                    this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '能够帮助孩子们在良好的环境中学习的更好让我很自豪',4 ,()=>{
                        this.zhuanchangAnim()
                    })
                }            
            })
        }else if(name == 'zhuozicheck'){
            this.leftNode.getChildByName('篮球羽毛球').active = true
            this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '孩子们的课间运动也要跟上',2, ()=>{
                if(this.leftzhibiIndex == 4){
                    this.showQiPao(this.leftNode.getChildByName('qiPaocheng'), '能够帮助孩子们在良好的环境中学习的更好让我很自豪',4 ,()=>{
                        this.zhuanchangAnim()
                    })
                }               
            })

        }


    }

    chuanglianClickEvent(event){
        let node = event.target
        cc.tween(node)
        .to(1.0,{x:700})
        .hide()
        .start()
        this.isKaiChuang = true
    }


    initParameters(){
        this.titleArrayConfig = this.jsonData.titleArray;
  
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs=[this.jsonData.tipsArray]
        Common.GameSubAnswerConfigs=[this.jsonData.answersArray]

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


    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.5})
            .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)
    }






    // update (dt) {}
}