import { _decorator, Node, Label} from 'cc';
import { BaseExp } from '../core/base/BaseExp';
import List from '../third/List';
import { autoBind } from '../extend/AutoBind';
import { userIns } from '../data/UserData';
import { Utils } from '../utils/Utils';
import MsgHints from '../utils/MsgHints';
import { StoreItem } from '../items/item/StoreItem';
import { Constants } from '../data/Constants';
import { stateMgr } from '../core/manager/StateManager';
import i18n from '../core/i18n/runtime-scripts/LanguageData';
import PlatformSystem from '../platform/PlatformSystem';
import { ITEM_TYPE, ResUtil } from '../utils/ResUtil';
import { audioMgr } from '../core/manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('StoreUI')
export class StoreUI extends BaseExp {
    @autoBind({ type: List, tooltip: "任务数据" })
    public coin_scrollView: List;
    @autoBind({ type: List, tooltip: "任务数据" })
    public diamond_scrollView: List;
    @autoBind({ type: Label, tooltip: "金币文本" })
    public gold_lable: Label;
    @autoBind({ type: Label, tooltip: "钻石文本" })
    public diamond_lable: Label;
    @autoBind({ type: Label, tooltip: "金币购买的list标题" })
    public coin_title_lable: Label;
    @autoBind({ type: Label, tooltip: "钻石购买的list标题" })
    public diamond_title_lable: Label;

    //金币数据
    private coinList:Array<any> = [];
    //钻石数据
    private diamondList:Array<any> = [];

    start() {
        this.hasAnim = false;
        this.closeOnBlank = false;
        //注册动态变化值
        stateMgr.registerUI(Constants.gold, this.gold_lable);
        stateMgr.registerUI(Constants.diamond, this.diamond_lable);
    }
    
    public show(...args: any[]){
        this.loadStoreData();
    }

    /**
     * 加载商店数据
     */
    public loadStoreData(){
        //重置所有广告次数
        const datas = Utils.clone(userIns.shopTable).map(e => ({ ...e, alreadyVideo: 0 }));
        this.coinList = datas.filter(e=>e.type === 1);
        if(this.coinList.length > 0){
           let coin = this.coinList[0];
           this.coin_title_lable.string = i18n.isZh ? coin.name : coin.name_lang;
        }
        this.coin_scrollView.numItems = this.coinList.length;
        this.diamondList = datas.filter(e=>e.type === 2);
        this.diamond_scrollView.numItems = this.diamondList.length;
        if(this.diamondList.length > 0){
           let diamond = this.diamondList[0];
           this.diamond_title_lable.string = i18n.isZh ? diamond.name : diamond.name_lang;
        }
    }

    /**
     * 设置金币购买的数据
     * @param item item节点
     * @param idx 数据信息
     */
    public setCoinItemData(item: Node, idx: number) {
        let com:StoreItem = item.getComponent(StoreItem);
        com.init(this.coinList[idx],(data:any,n: Node)=>{
            this.clicked(data,n)
        }); 
    }

    /**
     * 设置钻石购买的数据
     * @param item item节点
     * @param idx 数据信息
     */
    public setDiamondItemData(item: Node, idx: number) {
        let com:StoreItem = item.getComponent(StoreItem);
        com.init(this.diamondList[idx],this.clicked.bind(this)); 
    }

    /**
     * 看广告
     * @param data 看广告数据
     * @param clikTarget 道具节点
     */
    public clicked(data:any,clikTarget: Node){
        audioMgr.playOneShot(Constants.audios.click);
        const num: number = Number(data.price_2);
        PlatformSystem.platform.showRewardVideo((f) => {
            if(f) {
                data.alreadyVideo += 1;
                if(data.alreadyVideo >= num){
                    data.alreadyVideo = 0;
                    //达到看视频的次数 获得奖励
                    if(data.type == 1){
                        audioMgr.playOneShot(Constants.audios.buy);
                        userIns.data.gold += data.quantity;
                        ResUtil.flyAnim(ITEM_TYPE.Coin, clikTarget, this.gold_lable.node, 5, 50,(b) => {});
                    }else if(data.type == 2){
                        audioMgr.playOneShot(Constants.audios.buy);
                        userIns.data.diamond += data.quantity;
                        ResUtil.flyAnim(ITEM_TYPE.Diamond, clikTarget, this.diamond_lable.node, 5, 50,(b) => {});
                    }
                }else{
                    MsgHints.show(`Watch ${num - data.alreadyVideo} more times to purchase successfully!`);
                }
                if(data.type === 1){
                    this.coin_scrollView.updateAll();
                }else if(data.type === 2){
                    this.diamond_scrollView.updateAll();
                }
            }
        });
    }
}

