import Image = Laya.Image;
import FontClip = Laya.FontClip;
import Button = Laya.Button;
import Box = Laya.Box;
import Text = Laya.Text;
import  ShopView, { SkinGetForType } from "../View/ShopView"
// import SkinView from "../View/SkinView";
import {ServerManager, ArcherWorrior} from "../Net/ServerManager";
import { AccountManager } from "../Net/AccountManager";
import { WXSDK } from "../Net/WXSDK";
import GuangGao from "../Net/GuangGao";
import { AccountUser } from "../Net/PlayerData";
import { EventManager, EventState } from "./EventManager";



export default class  ShopItem extends Laya.Script{
    private static ins:ShopItem;
    public static get Instance():ShopItem{
        if(ShopItem.ins){
            return ShopItem.ins
        }
        else{
            return new ShopItem();
        }
    }
    constructor(){
        super();
        ShopItem.ins = this;
    }
    //正在使用
    private use:boolean = false;
   
    private selfobj: Laya.Box;
    private _select: Image;
    private _skin: Image;
    private using:Box;
    private click1_btn: Button;
    private skinGetForType: SkinGetForType;

    private _data: any;
    onAwake() {
        this.selfobj = this.owner as Box;
        this._select = this.selfobj.getChildByName("choose") as Image;
        this._skin = this.selfobj.getChildByName("icon") as Image;
        this.using = this.selfobj.getChildByName("used") as Box;
        this.click1_btn = this.selfobj.getChildByName("btn") as Button;
        this.click1_btn.clickHandler = new Laya.Handler(this, this._ClickHander);

        EventManager.EventOn(EventState.Select,this,this.ShowSelect);
    }
    private comeType:number;
    private instance:any;
    
    RefreshItem(any?,comeType?) {
        this.comeType=comeType;
        if(any)this.instance=any;
        else this.instance=ShopView.Instance;
        this._data = (this.selfobj.dataSource);
        // ShopView.Instance._data = this._data;
        var _id = this._data.id;
        
        var usingId = AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["1"];
        var usingId2 = AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["2"];
        var hasequip = AccountManager.Instance.curplayerData.HasEquip(_id);
        var require_type = this._data.require_type;
        this._skin.skin = "Game/res/Skin/" + this._data.icon + ".png";
        this._skin.rotation = 0;
        if (usingId == _id || usingId2 == _id) {
            this.skinGetForType = SkinGetForType.Using;
            this.showui();
            return;
        }
        if (hasequip) {
            this.skinGetForType = SkinGetForType.Geted;
            this.showui();
            return;
        }
        if (require_type == 2) {
            this.skinGetForType = SkinGetForType.Bycoin;
            this.showui();
            return;
        }
        if (require_type == 4 ) {
            this.skinGetForType = SkinGetForType.GetByActivity;
            this.showui();
            return;
        }
        if (require_type == 3) {
            this.skinGetForType = SkinGetForType.GetByVideo;
            this.showui();
            return;
        }
    }

    
    showui(){
        var type = this.skinGetForType;
        if(this._data.id == 1 || this._data.id == 50){
            this._select.visible = true;
        }
        else{
            this._select.visible = false;
        }
        // this._select.visible = type == SkinGetForType.Using;
        if(this.skinGetForType == SkinGetForType.Using){
            this.using.visible = true;
        }
        else{
            this.using.visible = false;
        }
    }

    ShowUi() {
        var type = this.skinGetForType;
        this._select.visible = type == SkinGetForType.Using;
        if(this.skinGetForType == SkinGetForType.Using){
            this.using.visible = true;
        }
        else{
            this.using.visible = false;
        }
    }
    _ClickHander() {
        this.instance._data = this._data;
        ShopView.Instance.RefreshButton(this.skinGetForType);
        this.instance.showroleId = this._data.id;
        this.ShowUi();
        this.ShowModel();
    }

    ShowModel() {
        EventManager.EventTrigger(EventState.Select);
    }
    ShowSelect() {
        if (!this._data){
            return;
        }
        this._select.visible = this.instance.showroleId == this._data.id;
    }
  
}
