import { _decorator, Component, Node, Prefab, LayoutComponent, instantiate, ScrollViewComponent } from 'cc';
import { GUIManager } from '../../../engines/gui/GUIManager';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { DataModelEventType } from '../../../engines/models/DataModelEventType';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
import { UIConst } from '../UIConst';
import { ShopItemRenderScript } from './ShopItemRenderScript';
const { ccclass, property } = _decorator;

@ccclass('ShopMediator')
export class ShopMediator extends GUIMediator {

    @property({
        type:Prefab
    })
    ListItemRenderPrefab:Prefab=null;


    @property({
        type:LayoutComponent
    })
    ListContext:LayoutComponent=null;

    @property({
        type:ScrollViewComponent
    })
    List:ScrollViewComponent=null;

    /**
     * 需要包含在内的武器
     */
    private include:number[]=[];

    OnShow(data?:any):void{
        super.OnShow(data);
        this.RefreshList(true);
        this.AddEvent();
    }

    OnHide():void{
        this.RemoveEvent();
    }
    
    /**
     * 返回按钮点击
     */
    public BackButtonClick():void{
        GUIManager.single.Show(UIConst.PREPARE_UI);
        this.HideSelf();
    }

    private AddEvent():void{
        GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged,0);
    }

    private RemoveEvent():void{
        GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED,this,this.GameModelPropertyChanged);
    }
    private GameModelPropertyChanged(key:string) {
        if(key==GamePropertys.WeaponCell){
            this.RefreshList(false);
        }
    }
    
    private RefreshList(srollToZ:boolean):void{
        //根据当前枪生成包含列表
        this.buildInclude();

        let itemView:Node;
        let dataList:any[]=this.GetListData();
        if(dataList.length!=this.ListContext.node.children.length){
            if(dataList.length<this.ListContext.node.children.length){
                //删除多余的
                while(dataList.length<this.ListContext.node.children.length){
                    this.ListContext.node.removeChild(this.ListContext.node.children[0]);
                }
            }else{
                //添加缺少的
                while(dataList.length>this.ListContext.node.children.length){
                    itemView=instantiate(this.ListItemRenderPrefab);
                    this.ListContext.node.addChild(itemView);
                }
            }
        }
        let itemScript:ShopItemRenderScript;
        let count:number=dataList.length;
        for (let index = 0; index < count; index++) {
            itemView=this.ListContext.node.children[index];
            itemScript=itemView.getComponent(ShopItemRenderScript);
            if(itemScript==null){
                throw new Error("商城列表项未挂载ShopItemRenderScript脚本!");
            }
            itemScript.UpdateItemRender(dataList[index]);
        }
        if(srollToZ){
            this.List.scrollToTop(0.1);
        }
    }
    private buildInclude():void{
        this.include.length=0;
        let currentWeaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
        let weaponConfig:any;
        //低级6个
        for (let index = 1; index <=6; index++) {
            weaponConfig=GameConfigManager.GetWeaponConfigByLevel(currentWeaponConfig.level-index);
            if(weaponConfig!=null){
                this.include.push(weaponConfig.id);
            }
        }
        //当前
        this.include.push(GameModel.single.synthesisMaxWeaponId);
        //高级3个
        for(let index=1;index<4;index++){
            weaponConfig=GameConfigManager.GetWeaponConfigByLevel(currentWeaponConfig.level+index);
            if(weaponConfig!=null){
                this.include.push(weaponConfig.id);
            }
        }
    }

    /**
     * 根据当前合成过的最大枪来计算列表数据
     */
    private GetListData():any[]{
        let result:any[]=[];
        let config:any=GameConfigManager.GetShopConfig(GameModel.single.synthesisMaxWeaponId);
        let weaponConfig:any;
        //解锁线
        let unlockLevel:number;
        if(config.unsealedGunId!=undefined){
            weaponConfig=GameConfigManager.GetWeaponConfig(config.unsealedGunId);
            unlockLevel=weaponConfig.level;
        }else{
            unlockLevel=-1;
        }
        let shopList:any[]=GameConfigManager.ShopList;
        shopList.sort((a,b)=> a.index-b.index);
        let itemData:any;

        for (let index = 0; index < shopList.length; index++) {
            const element = shopList[index];
            if(this.include.indexOf(element.id)<0){
                continue;
            }
            weaponConfig=GameConfigManager.GetWeaponConfig(element.id);
            itemData={
                id:element.id,
                index:element.index
            }
            //buyType 购买方式 0金币购买  1宝石购买 2 视频购买
            if(config.diamondPurchaseId==undefined){
                itemData.buyType=0;
            }else if(config.diamondPurchaseId.indexOf(element.id)>=0){
                itemData.buyType=1;
            }else if(config.videoId==undefined){
                itemData.buyType=0
            }else if(config.videoId==element.id){
                itemData.buyType=2;
            }else{
                itemData.buyType=0;
            }
            //state 0 未解锁  1 已解锁
            if(unlockLevel<0||weaponConfig.level>unlockLevel){
                itemData.state=0;
            }else{
                itemData.state=1;
            }
            result.push(itemData);
        }
        return result;
    }

    start () {
        // Your initialization goes here.
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
