import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
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";


const {ccclass, property} = cc._decorator;

@ccclass
export default class ChaChangZhaoMu 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()
    }
    start () {
        this.initBtnPeriod()
        this.refreshView()
        
    }

    update (dt) {
        this._dt += dt
        if(this._dt >= 1){
            this._dt = 0
            this.refreshView()
        }
    }
    initBtnPeriod(){
        let maxLength = 3
        let localZhaoMuData = User.getChaChangZhaoMuData() 

        for(var i=0;i<maxLength;i++){
            let skeleton = this.pageContent.getChildByName("page_"+i).getChildByName("skeleton")
            if(localZhaoMuData.unLockTab.indexOf(i) >= 0){
                skeleton.getComponent(sp.Skeleton).setAnimation(0,(i+1)+'',true)
            }else{
                skeleton.getComponent(sp.Skeleton).setAnimation(0,(i+1)+'黑',true)
            }
        }
    }
    refreshView(){
        let maxLength = 3
        let localZhaoMuData = User.getChaChangZhaoMuData() 
        for(var i=0;i<maxLength;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.unLockTab.indexOf(i) >= 0){
                    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.zhaoMuTime)}后刷新`
                }else{
                    btnZhaoMuAD.getComponent(cc.Button).interactable = true
                    btnZhaoMuAD.active = true

                    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("CommonRes/sound/按键点击")
        if(this.isPlaying){
            return
        }
        this.isPlaying = true
        let skeleton = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("skeleton")
        let btnZhaoMuAD = this.pageContent.getChildByName("page_"+this.curPageIndex).getChildByName("btnZhaoMuAD")

        let tab = {
            onClose: (finish)=>{
                if (finish) {
                    
                    Common5.ReportDY("inLevel", '茶场招募-AD-茶场招募一次');
                    skeleton.getComponent(sp.Skeleton).setAnimation(0,(this.curPageIndex+1)+'',true)
                    btnZhaoMuAD.active = true
                    btnZhaoMuAD.getComponent(cc.Button).interactable = false
                    this.setZhaoMuData(this.curPageIndex)
                    this.isPlaying = false

                    let localZhaoMuData = User.getChaChangZhaoMuData() 
                    let zhaoMuBeiShu = localZhaoMuData.zhaoMuBeiShu
                    PrefabManage.showTextTips(`收益已提升至${zhaoMuBeiShu}倍`)
                }
                else{
                    this.isPlaying = false
                   
                    Common5.showTips_custom("广告未观看完");
                }
            },onFailed:()=>{

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    setZhaoMuData(addId){
        let tab = User.getChaChangZhaoMuData() 
        let zhaoMuTime = tab.zhaoMuTime + 3600
        let unLockTab = tab.unLockTab
        if(unLockTab.indexOf(addId) < 0){
            unLockTab.push(addId)
        }
        let zhaoMuBeiShu = 1
        if(unLockTab.length == 1){
            zhaoMuBeiShu = 2
        }else if(unLockTab.length == 2){
            zhaoMuBeiShu = 4
        }else if(unLockTab.length == 3){
            zhaoMuBeiShu = 8
        }

        User.setChaChangZhaoMuData(unLockTab,zhaoMuTime,zhaoMuBeiShu)
        EventMgr.emitEvent_custom(ryw_Event.refreshZhaoMuTimes);
    }
    
    leftClick(){
        Common5.playEffect("CommonRes/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("CommonRes/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
    }
}