import { _decorator, Component, Label, Node, Animation } from 'cc';
import { base_ui } from '../fw/base_ui';
import { userDataManager } from '../manager/userDataManager';
import { tools } from '../tools';
import { SdkUtil } from '../sdkUtil';
import { config } from '../config';
import { GameManager } from '../GameManager';
const { ccclass, property } = _decorator;

@ccclass('unLock_view')
export class unLock_view extends base_ui {
    @property(Node) btn_close:Node = null
    @property(Node) btn_look_video:Node = null
    @property(Node) lab_look_video:Node = null
    @property(Node) lab_des:Node = null
    private m_cur_count:number = 0
    private m_max_count:number = 3
    private m_lookVideo_finish_cb = null
    start() {
        this.onButtonListen(this.btn_close, ()=>{
            this.stopBtnLookVideoAni()
            this.closeSelf()
        })
        this.onButtonListen(this.btn_look_video, this.onLookVideo.bind(this))
        this.playBtnLookVideoAni()
    }

    initView(lookVideo_finish_cb) {
        this.m_lookVideo_finish_cb = lookVideo_finish_cb
        
        this.m_cur_count = userDataManager.getUserFreeAdsData().look_video_count
        let sys_config = tools.sys_config
        this.m_max_count = sys_config.free_ads_number
        let time = 0 
        if(sys_config.free_game_time >0) {
            time = sys_config.free_game_time / 3600
        } 
        if(time<1) {
            time = parseFloat(time.toFixed(1))
        }
        // console.log('time=',time)
        this.lab_look_video.getComponent(Label).string = '看视频' + this.m_cur_count + '/' + this.m_max_count
        this.lab_des.getComponent(Label).string = '观看' + this.m_max_count + '次视频，获取' + time + '小时无限挑战机会'
    }

    private onLookVideo() {
        if(this.m_cur_count>=this.m_max_count) {
            this.finishLookVideo()
            return
        }
        GameManager.showVideoAd(config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO, (res)=>{
            this.m_cur_count++
            this.lab_look_video.getComponent(Label).string = '看视频' + this.m_cur_count + '/' + this.m_max_count
            if(this.m_cur_count<this.m_max_count) {
                let free_ads_data = userDataManager.getUserFreeAdsData()
                free_ads_data.look_video_count = this.m_cur_count
                userDataManager.saveUserFreeAdsData()
            } else {
                this.finishLookVideo()
            }
        })
    }

    private finishLookVideo() {
        let cur_time = new Date().getTime()
        let free_ads_data = userDataManager.getUserFreeAdsData()
        free_ads_data.start_date_time = cur_time
        free_ads_data.look_video_count = 0
        free_ads_data.is_look_video_infinite_count_reward = true
        userDataManager.saveUserFreeAdsData()
        if(this.m_lookVideo_finish_cb) {
            this.m_lookVideo_finish_cb(this)
        }
        this.closeSelf()
    }

    closeSelf() {
        this.close()
    }

    private playBtnLookVideoAni() {
        this.btn_look_video.getComponent(Animation).play()
    }

    private stopBtnLookVideoAni() {
        this.btn_look_video.getComponent(Animation).stop()
    }
}

