import GameReport from "../../FrameWork/Report/ZyZyReport";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import { DateUtils } from "../../FrameWork/Util/DateUtils";
import Common5 from "../../Platform/th/Common5";
import UserManager from "../Manager/UserManager";
import PrefabManage from "../PrefabManager/PrefabManage";
import GameBase from "../SCommon/GameBase";
import MeiNvZhaoMuManager from "./MeiNvZhaoMuManager";


enum PeriodNum{
    Period_0,
    Period_1,
    Period_2,
    Period_3
}
const {ccclass, property} = cc._decorator;

@ccclass
export default class MeiNvZhaoMu extends GameBase {

    @property(cc.PageView)
    pageView:cc.PageView=null

    @property(cc.Node)
    pageContent:cc.Node=null

    @property(cc.Node)
    timeNode:cc.Node=null
    
    // @property(cc.Node)
    // btnZhaoMu:cc.Node=null

    // @property(cc.Node)
    // btnZhaoMuAD:cc.Node=null
    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    btnLeftNode: cc.Node = null;

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

    curPageIndex = 0
    // onLoad () {}
    _dt:number = 0
    btnPeriod = []
    isPlaying:boolean = false
    curTypeId:number = 0
    onLoad () {
        super.onLoad()

    }
    onDestroy(){
        super.onDestroy()
    }
    //0快递,1松花江,2下水道
    initView(typeId){
        this.curTypeId = typeId

        this.initBtnPeriod()
        this.refreshView()
    }
    start () {
        // MeiNvZhaoMuManager.initManager()
        
    }

    update (dt) {
        this._dt += dt
        if(this._dt >= 1){
            this._dt = 0
            this.refreshView()
        }
    }
    initBtnPeriod(){
        let localZhaoMuData = MeiNvZhaoMuManager.getMeiNvZhaoMuData() 
        let AllConfig = MeiNvZhaoMuManager.getAllConfig()

        for(var i=0;i<AllConfig[this.curTypeId].length;i++){
            this.btnPeriod[i] = PeriodNum.Period_2
            let skeleton = this.pageContent.getChildByName("page_"+i).getChildByName("skeleton")
            // if(localZhaoMuData[i].isZhaoMuZhong){
            //     skeleton.getComponent(sp.Skeleton).animation = "daijib"
            // }else{
            //     skeleton.getComponent(sp.Skeleton).animation = "daijia"
            // }
            if(localZhaoMuData[this.curTypeId].unLockTab.indexOf(i) >= 0){

                // Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', '呼吸', true, AllConfig[this.curTypeId][i].skinIndex)

                skeleton.getComponent(sp.Skeleton).setAnimation(0,'呼吸',true)
                skeleton.getComponent(sp.Skeleton).setSkin(AllConfig[this.curTypeId][i].skinIndex)
            }else{

                // Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', '黑影', true, AllConfig[this.curTypeId][i].skinIndex)

                skeleton.getComponent(sp.Skeleton).setAnimation(0,'黑影',true)
                skeleton.getComponent(sp.Skeleton).setSkin(AllConfig[this.curTypeId][i].skinIndex)
            }
        }
    }
    refreshView(){
        let localZhaoMuData = MeiNvZhaoMuManager.getMeiNvZhaoMuData() 
        let AllConfig = MeiNvZhaoMuManager.getAllConfig()
        for(var i=0;i<AllConfig[this.curTypeId].length;i++){
            if(i == this.curPageIndex){
                // let skeleton = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("skeleton")
                // let btnZhaoMu = this.pageContent.getChildByName("page_"+i).getChildByName("btnZhaoMu")
                let btnZhaoMuAD = this.pageContent.getChildByName("page_"+i).getChildByName("btnZhaoMuAD")

                if(localZhaoMuData[this.curTypeId].unLockTab.indexOf(i) >= 0){
                    this.btnPeriod[i] = PeriodNum.Period_3
                    btnZhaoMuAD.getComponent(cc.Button).interactable = false
                    btnZhaoMuAD.active = true
                    // btnZhaoMu.active = false
                    this.timeNode.active = true
                    this.timeNode.getChildByName("timeStr").getComponent(cc.Label).string = `${DateUtils.formatTime_custom(localZhaoMuData[this.curTypeId].zhaoMuTime)}后刷新`
                }else{
                    // if(this.btnPeriod[i] == PeriodNum.Period_0){
                    //     // btnZhaoMu.active = true
                    //     // btnZhaoMu.getChildByName("labM").getComponent(cc.Label).string = Common5.getNumberChangeHanzi(AllConfig[i].buyPrice1,'1',1)+''
                    //     // btnZhaoMu.attr({bindMoney:AllConfig[i].buyPrice1})
                    //     btnZhaoMuAD.active = false
                    // }else if(this.btnPeriod[i] == PeriodNum.Period_1){
                    //     // btnZhaoMu.active = true
                    //     // btnZhaoMu.getChildByName("labM").getComponent(cc.Label).string = Common5.getNumberChangeHanzi(AllConfig[i].buyPrice2,'1',1)+''
                    //     // btnZhaoMu.attr({bindMoney:AllConfig[i].buyPrice2})
                    //     btnZhaoMuAD.active = false
                    // }else 
                    if(this.btnPeriod[i] == PeriodNum.Period_2){
                        btnZhaoMuAD.getComponent(cc.Button).interactable = true
                        btnZhaoMuAD.active = true
                        // btnZhaoMu.active = false
                    }
                    this.timeNode.active = false
                    this.timeNode.getChildByName("timeStr").getComponent(cc.Label).string = ``
                }
                break
            }
        }
    }
    socrllPageEvent(){
        console.log('socrllPageEvent++++++++++++++++')
        let page = this.pageView.getCurrentPageIndex()
        console.log(page, 'page')
        this.curPageIndex = page
        this.refreshView()
    }
    onBtnZhaoMuADClick(){
        //Common5.playEffect("sound/按键点击")
        if(this.isPlaying){
            return
        }
        this.isPlaying = true
        let skeleton = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("skeleton")
        // let btnZhaoMu = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("btnZhaoMu")
        let btnZhaoMuAD = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("btnZhaoMuAD")

        let AllConfig = MeiNvZhaoMuManager.getAllConfig()


        let tab = {
            onClose: (finish)=>{
                if (finish) {
                

                    this.reportKey(()=>{
                        GameReport.ADReport('招募'+this.curPageIndex,1)
                    })
                    
                    Common5.ReportDY("inLevel", '招募-AD-美女招募一次');
                    skeleton.getComponent(sp.Skeleton).setAnimation(0,"呼吸",true)
                    // Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', '呼吸', true, AllConfig[this.curTypeId][this.curPageIndex].skinIndex)
                    
                    this.btnPeriod[this.curPageIndex] = PeriodNum.Period_3
                    // btnZhaoMu.active = false
                    btnZhaoMuAD.active = true
                    btnZhaoMuAD.getComponent(cc.Button).interactable = false
                    MeiNvZhaoMuManager.setMeiNvZhaoMuData(this.curTypeId,this.curPageIndex)
                    this.isPlaying = false

                    let localZhaoMuData = MeiNvZhaoMuManager.getMeiNvZhaoMuData() 
                    let zhaoMuBeiShu = localZhaoMuData[this.curTypeId].zhaoMuBeiShu
                    PrefabManage.showTextTips(`收益已提升至${zhaoMuBeiShu}倍`)
                }
                else{
                    this.isPlaying = false
                    this.reportKey(()=>{
                        GameReport.ADReport('招募'+this.curPageIndex,0)
                    })
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    onBtnZhaoMuClick(event){
        //Common5.playEffect("sound/按键点击")
       

        this.reportKey(()=>{
            GameReport.BtnsReport('投资'+this.curPageIndex)
        })


        if(this.isPlaying){
            return
        }
        
        if(!this.subMoney(event.target.bindMoney)){
            return
        }

        let AllConfig = MeiNvZhaoMuManager.getAllConfig()

        this.isPlaying = true
        let skeleton = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("skeleton")
        let btnZhaoMu = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("btnZhaoMu")
        let btnZhaoMuAD = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("btnZhaoMuAD")
        if(this.btnPeriod[this.curPageIndex] == PeriodNum.Period_0){
            //skeleton.getComponent(sp.Skeleton).setAnimation(0,"zhezhao1",false)

            Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', 'zhezhao1', false,AllConfig[this.curTypeId][this.curPageIndex].skinIndex)
                    
            cc.tween(this.node)
                .delay(0.5)
                .call(()=>{
                    // skeleton.getComponent(sp.Skeleton).setAnimation(0,"zhezhao1b",true)

                    Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', 'zhezhao1b', true, AllConfig[this.curTypeId][this.curPageIndex].skinIndex)
                 

                    this.btnPeriod[this.curPageIndex] = PeriodNum.Period_1
                    this.isPlaying = false
                })
            .start()
        }else if(this.btnPeriod[this.curPageIndex] == PeriodNum.Period_1){
            //skeleton.getComponent(sp.Skeleton).setAnimation(0,"zhezhao2",false)
            Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', 'zhezhao2', false,AllConfig[this.curTypeId][this.curPageIndex].skinIndex)
            cc.tween(this.node)
                .delay(0.5)
                .call(()=>{
                    //skeleton.getComponent(sp.Skeleton).setAnimation(0,"zhezhao2b",true)
                    Common5.playRemoteSpine(skeleton,'Spine/meinvzhaomu','人才招聘', 'zhezhao2b',true,AllConfig[this.curTypeId][this.curPageIndex].skinIndex)
                    this.btnPeriod[this.curPageIndex] = PeriodNum.Period_2
                    
                    btnZhaoMu.active = false
                    btnZhaoMuAD.active = true
                    this.isPlaying = false
                })
            .start()
        }
    }
    leftClick(){
        //Common5.playEffect("sound/按键点击")
        this.curPageIndex--
        if(this.curPageIndex<0){
            this.curPageIndex = 0
        }
        if(this.curPageIndex>=1 && this.curPageIndex<2){
            this.btnLeftNode.active = true
            this.btnRightNode.active = true
        }
        this.pageView.scrollToPage(this.curPageIndex,0.6) 
        if(this.curPageIndex==0){
            this.btnLeftNode.active = false
            this.btnRightNode.active = true
        }
        this.refreshView()
    }

    rightClick(){
        //Common5.playEffect("sound/按键点击")
        this.curPageIndex++
        if(this.curPageIndex>2){
            this.curPageIndex = 2
        }
        if(this.curPageIndex>=1 && this.curPageIndex<2){
            this.btnLeftNode.active = true
            this.btnRightNode.active = true
        }
        this.pageView.scrollToPage(this.curPageIndex,1) 
        if(this.curPageIndex==2){
            this.btnLeftNode.active = true
            this.btnRightNode.active = false
        }
    }
    subMoney(money){
        if(money > UserManager.getCurMoney()){
            PrefabManage.showTextTips("余额不足");
            return false
        }
        UserManager.subMoney(money)
        return true
    }
}