import { ui } from "../ui/layaMaxUI";
import { DispatcherManager, EventType } from "../Game/DispatcherManager";
import SkinItem from "./Com/SkinItem";
import {DisplayerCarController} from "../Net/DisplayerCarController";
import {ViewManager,ViewType } from "../Net/ViewManager";
import { PlayerManager } from "../Game/PlayerManager";
import Image = Laya.Image;
import UserModel, { UserAttr } from "../Net/UserModel";
import { NetInfoManager } from "../Game/NetInfoManager";
import NetManager, { PROTOCOLS } from "../Net/NetManager";
import {GuangGao} from "../Net/GuangGao";
import { WXSdk } from "../Net/WXSdk";
import {MainRoleManager} from "../Manager/MainRoleManager";
import { FYHD_Sdk } from "../Net/FYHD_Sdk";
export class SKinShopView extends ui.SkinShopViewUI implements SC {
    private static instance: SKinShopView;
    static get Instance() {
        return SKinShopView.instance;
    }
    constructor() {
        super();
        SKinShopView.instance = this;
        this.Resize();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.Resize);
    }
    Resize() {
        this.height = Laya.stage.height;
    }
    OnShow(data?: any) {
        this.RefreshData();
        this.RegButton();
        this.RefreshUI();
        WXSdk.AldEvent("打开皮肤界面");
    }
    private oignPlayerid: number;
    private operateIndex: number;
    RefreshData() {
        this.oignPlayerid = PlayerManager.Instance.equipId;
        this.operateIndex = this.oignPlayerid;
    }
    RefreshUI(data?: any) {
        this.SetBtnUi(this.operateIndex);
        this.RefreshBtnUi(this.operateIndex);
        DisplayerCarController.Instance.InitDis(this.viewPort);
    }
    

    

    private coinValue: number;
    RefreshBtnUi(index) {
        if (PlayerManager.Instance.equipId == index) {
            this.using.visible = true;
            this.use.visible = false;
            this.coin.visible = false;
            this.video.visible = false;
            return;
        }
        if (UserModel.Instance.CurModel.HasTheme(index)) {
            this.using.visible = false;
            this.use.visible = true;
            this.coin.visible = false;
            this.video.visible = false;
            return;
        }
        let data = NetInfoManager.Instance.GetTheme(index);
        if (data.require_type == 2) {
            this.using.visible = false;
            this.use.visible = false;
            this.coin.visible = true;
            this.video.visible = false;
            this.coinValue = Number(data.require_num);
            (this.coin.getChildAt(0).getChildAt(0) as Laya.Text).text = this.coinValue.toString();
            // this.coin.getChildByName
            return;
        }
        if (data.require_type == 3) {
            this.using.visible = false;
            this.use.visible = false;
            this.coin.visible = false;
            this.video.visible = true;
            (this.video.getChildAt(0).getChildAt(0) as Laya.Text).text = UserModel.Instance.CurModel.GetThemeProgress(data.id) + "/" + data.require_num;
            return;
        }
    }

    SetBtnUi(index) {
        if (index == 1) {
            (this.leftBtn.getChildByName("leftFalse") as Image).visible = true;
            (this.leftBtn.getChildByName("leftTrue") as Image).visible = false;
            (this.rightBtn.getChildByName("rightFalse") as Image).visible = false;
            (this.rightBtn.getChildByName("rightTrue") as Image).visible = true;
            return;
        }
        if (index == 8) {
            (this.leftBtn.getChildByName("leftFalse") as Image).visible = false;
            (this.leftBtn.getChildByName("leftTrue") as Image).visible = true;
            (this.rightBtn.getChildByName("rightFalse") as Image).visible = true;
            (this.rightBtn.getChildByName("rightTrue") as Image).visible = false;
            return;
        }
        (this.leftBtn.getChildByName("leftFalse") as Image).visible = false;
        (this.leftBtn.getChildByName("leftTrue") as Image).visible = true;
        (this.rightBtn.getChildByName("rightFalse") as Image).visible = false;
        (this.rightBtn.getChildByName("rightTrue") as Image).visible = true;
    }

    ClickClose() {
        DisplayerCarController.Instance.Reset();
       if (this.oignPlayerid != PlayerManager.Instance.equipId) {
            MainRoleManager.Instance.ChangeModel();
       }
       ViewManager.Instance.CloseView(ViewType.SKinShopView);
   }
    ClickVideo() {
        GuangGao.ShowVideo((done) => {
            if (done) {
                WXSdk.AldEvent("商店中看视屏获得皮肤");
                this.VideoGet();
            }
        })
    };
    VideoGet() {
        var param = [{ "t": UserModel.Instance.CurModel.ServerTime.toString() }, { "id": this.operateIndex }, { "channel": "video" }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.themefission, param,
            (data: any) => {
                var usermodel = UserModel.Instance.CurModel;
                PlayerManager.Instance.equipId = Number(usermodel.GetUserAttr(UserAttr.equip_themes)["1"]);
                this.RefreshBtnUi(this.operateIndex);
            },
            (data) => {

            });
    }
    Buy() {
        var param = [{ "id": this.operateIndex }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.Buy, param,
            (data: any) => {
                var usermodel = UserModel.Instance.CurModel;
                PlayerManager.Instance.equipId = Number(usermodel.GetUserAttr(UserAttr.equip_themes)["1"]);
                DispatcherManager.event(EventType.Event.SetMonery, UserModel.Instance.CurModel.GetUserAttr(UserAttr.money));
                this.RefreshBtnUi(this.operateIndex);
            },
            (data) => {

            });
    }
    Equip() {
        var param = [{ "id": this.operateIndex }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.Equip, param,
            (data: any) => {
                var usermodel = UserModel.Instance.CurModel;
                PlayerManager.Instance.equipId = Number(usermodel.GetUserAttr(UserAttr.equip_themes)["1"]);
                this.RefreshBtnUi(this.operateIndex);
            },
            (data) => {

            });
    }

    RegButton() {
        this.ExitBtn.visible = FYHD_Sdk.iswhite;
        this.ExitBtn.clickHandler = new Laya.Handler(this,this.ClickExit);
        this.CloseBtn.visible = false;
        Laya.timer.once(3000,this,()=>{
            this.CloseBtn.visible = true;
            this.CloseBtn.clickHandler = new Laya.Handler(this,this.ClickClose);
        })
        this.leftBtn.clickHandler = Laya.Handler.create(this, () => {
            if (this.operateIndex == 1) return;
            this.operateIndex--;
            this.SetBtnUi(this.operateIndex);
            this.RefreshBtnUi(this.operateIndex);
            DisplayerCarController.Instance.CreatePlayer(this.operateIndex);

        }, null, false);
        this.rightBtn.clickHandler = Laya.Handler.create(this, () => {
            if (this.operateIndex == 8) return;
            this.operateIndex++;
            this.SetBtnUi(this.operateIndex);
            this.RefreshBtnUi(this.operateIndex);
            DisplayerCarController.Instance.CreatePlayer(this.operateIndex);
        }, null, false);
        this.use.CZ = () => {
            this.Equip();
        };
        this.coin.CZ = () => {
            if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.coinValue)ViewManager.Instance.ShowTips("金币不足!",2)
            else
                this.Buy();
        };
        this.video.CZ = () => {
            this.ClickVideo();
        };
    }
    ClickExit(){
        ViewManager.Instance.AddView(ViewType.ExitView);
    }

    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.Resize);
        SKinShopView.instance = null;
        WXSdk.AldEvent("关闭皮肤界面");
    }

}