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

@ccclass('play_list_item')
export class play_list_item extends Component {
    @property(Node) item_bg;
    @property(Node) lab_cur_level;
    @property(Node) mask_layer;
    @property(Node) btn_lock_coin;
    @property(Node) lab_coin_number;

    @property(Node) btn_lock_video:Node;
    @property(Node) lab_video_number:Node;

    @property(Node) btn_sub_coin:Node;
    @property(Node) lab_consume_coin:Node;
    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){
        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){

                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;
                    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.btn_sub_coin.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_call_back!=null){

                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;
                    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.btn_sub_coin.active = false;
        this.btn_lock_coin.on(Node.EventType.TOUCH_START,()=>{
            gameManager.playBtnSound()
            if(gameManager.get_user_coin()>=this.m_data.coin_number){
                tools.showDialog(`使用${this.m_data.coin_number}金币解锁${this.m_data.name}`,()=>{
                    gameManager.sub_coin(this.m_data.coin_number);
                    let data = gameManager.get_cur_level_by_categoryid(this.m_data.id);
                    data.is_lock_game_play = 1;
                    gameManager.set_unlock_levels_data(data);
                    gameManager.sync_data((is_sync)=>{},config.sync_data_type.LEVELS)
                    ClientEvent.dispatchEvent(config.EVENT_MSG.ON_UPDATE_HOME_VIEW)
                })
            }else{
                this.showViewLack()
            }

      
        },this);
        this.btn_lock_video.on(Node.EventType.TOUCH_END,()=>{           
            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;
                    if(data.look_video_number>=this.m_data.unlock_ads_number){
                        data.is_lock_game_play = 1;
                    }
                    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.m_data = data;
        this.updateView();
        this.hide();
    }

    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){
            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;
                }
            }

        }
    }

    public hide(){
        this.unscheduleAllCallbacks()
        this.node.position = new Vec3(-500,this.node.position.y,this.node.position.z)
        this.node.getComponent(Animation).stop()
    }

    public play(time:number,_index:number){
        let clip = this.node.getComponent(Animation).clips[_index]
        let list = clip.tracks[0].channels()[1].curve._values;
        let pos_y = this.node.position.y;
        for (let index = 0; index < list.length; index++) {
            clip.tracks[0].channels()[1].curve._values[index].value = pos_y
        }
        this.scheduleOnce(()=>{
            this.node.getComponent(Animation).play(`show_play_item_animation${_index}`)
        },time)
    }
}


