import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { server_play_list_data_item } from '../data/server_play_list_data';
import { gameManager } from '../gameManager';
import { config } from '../config';
import { tools } from '../tools';
import { ClientEvent } from '../framework/clientEvent';
import { Lack } from '../dialog/Lack';
import { SdkUtil } from '../framework/sdkUtil';
import { StatisticsManager } from '../framework/statisticsManager';
import { unlock_play_dialog } from './unlock_play_dialog';
const { ccclass, property } = _decorator;

@ccclass('play_page_list_item')
export class play_page_list_item extends Component {
    @property(Node) lab_level: Node = null;
    @property(Node) mask_layer: Node = null;

    private m_data: server_play_list_data_item = null;
    private m_call_back = null;

    public initView(data: server_play_list_data_item, sf: SpriteFrame, call_back: any) {
        this.m_call_back = call_back
        this.node.getComponent(Sprite).spriteFrame = sf
        this.node.on(Node.EventType.TOUCH_END, () => {
            if (this.m_call_back != null) {
                console.log(config.get_play_type_name(this.m_data.id))
                if (this.m_data.id === config.PLAY_TYPE.JI_YI_LI || this.m_data.id === config.PLAY_TYPE.DOU_DI_ZHU) {
                    if (gameManager.get_user_coin() >= this.m_data.consume_coin) {
                        gameManager.sub_coin(this.m_data.consume_coin)
                        this.m_call_back(this.m_data);
                    } else {
                        this.showViewLack()
                    }
                } else {
                    this.m_call_back(this.m_data);
                }
            }

        },this)

        this.mask_layer.on(Node.EventType.TOUCH_END, ()=> {
            gameManager.playBtnSound()
            gameManager.addTopView(config.PREFAB_PATH.unlock_play_dialog, (node:Node)=>{
                let data = gameManager.get_cur_level_by_categoryid(this.m_data.id);
                let look_video_string = data.look_video_number + '/' + this.m_data.unlock_ads_number
                node.getComponent(unlock_play_dialog).initView(`解锁${this.m_data.name}?`, this.m_data.coin_number, look_video_string, (dialog_view:unlock_play_dialog)=> {
                    if(gameManager.get_user_coin()>=this.m_data.coin_number){
                    } else {
                        dialog_view.close()
                        this.showViewLack()
                    }
                }, (dialog_view:unlock_play_dialog)=> {
                    SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Earn_coins_AdUnitId,(res)=>{
                        if(res.isEnded){
                            let data = gameManager.get_cur_level_by_categoryid(this.m_data.id);
                            data.look_video_number+=1;
                            let look_video_string = data.look_video_number + '/' + this.m_data.unlock_ads_number
                            dialog_view.setupLookVideo(look_video_string)
                            if(data.look_video_number>=this.m_data.unlock_ads_number){
                                data.is_lock_game_play = 1;
                                dialog_view.close()
                            }
                            gameManager.set_unlock_levels_data(data);
                            gameManager.sync_data((is_sync)=>{},config.sync_data_type.LEVELS)
                            if(data.is_lock_game_play===1){
                                ClientEvent.dispatchEvent(config.EVENT_MSG.ON_UPDATE_HOME_VIEW)
                            }else{
                                this.updateView()
                            }
                        }
                        // 统计-激励视频广告
                        let collect_data = StatisticsManager.get_collect_data(this.m_data.id, 0, 0, res, config.STATISTICS_ACTION_TYPE.JIE_SUO)
                        StatisticsManager.request_collect_rewardVideoData(collect_data)
                    })
                })
            })
        },this)

        this.m_data = data;
        this.updateView()
    }

    public showViewLack() {
        gameManager.addTopView(config.PREFAB_PATH.Lack,(node:Node)=>{
            node.getComponent(Lack).initView(null,(lack:Lack)=>{
                SdkUtil.showVideoAd(config.rewardVideoAdUnitId.Earn_coins_AdUnitId,(res)=>{
                    if(res.isEnded){
                        lack.close()
                        ClientEvent.dispatchEvent(config.EVENT_MSG.ON_SHOW_REWARD_VIEW)
                    }
                    // 统计-激励视频广告
                    let collect_data = StatisticsManager.get_collect_data(0, 0, 0, res, config.STATISTICS_ACTION_TYPE.ZHUAN_JIN_BI)
                    StatisticsManager.request_collect_rewardVideoData(collect_data)
                })
            });
        })
    }

    public updateView() {
        let data = gameManager.get_cur_level_by_categoryid(this.m_data.id);
        if (data != null) {
            this.lab_level.getComponent(Label).string = `${data.level}/${this.m_data.barrier_number}`
            if (data.is_lock_game_play === 1) {
                this.mask_layer.active = false;
            } else {
                this.mask_layer.active = true;
            }
            // if(data.is_lock_game_play===1){
            //     this.mask_layer.active = false;
            // }else{
            //     this.mask_layer.active = true;
            // }
            // if(gameManager.get_user_coin()>=this.m_data.coin_number){
            //     this.btn_lock_coin.getComponent(Sprite).grayscale = false;
            // }else{
            //     this.btn_lock_coin.getComponent(Sprite).grayscale = true;
            // }
            // if(this.m_data.unlock_ads_number>0){
            //     this.btn_lock_video.active = true;
            //     this.lab_video_number.getComponent(Label).string = `${data.look_video_number}/${this.m_data.unlock_ads_number}`
            // }else{
            //     this.btn_lock_video.active = false;
            // }
            // this.lab_coin_number.getComponent(Label).string = this.m_data.coin_number+"";
            // this.lab_cur_level.getComponent(Label).string = `${data.level}/${this.m_data.barrier_number}` 

            // if(this.m_data.id===config.PLAY_TYPE.JI_YI_LI||this.m_data.id===config.PLAY_TYPE.DOU_DI_ZHU){
            //     this.btn_sub_coin.active = true;
            //     this.item_bg.active = false;
            //     this.lab_consume_coin.getComponent(Label).string = `-${this.m_data.consume_coin}`
            //     if(gameManager.get_user_coin()>=this.m_data.consume_coin){
            //         this.btn_sub_coin.getComponent(Sprite).grayscale = false;
            //     }else{
            //         this.btn_sub_coin.getComponent(Sprite).grayscale = true;
            //     }
            // }

        }
    }
}

