import { ui } from "../ui/layaMaxUI";
import { DispatcherManager, EventType } from "../GameLogic/DispatcherManager";
import SkinItem from "./Com/SkinItem";
import DisplayerCarController from "../game_module/DisplayerCarController";
import ViewManager, { ViewType } from "../game_module/ViewManager";
import { PlayerManager } from "../GameLogic/PlayerManager";
import Image = Laya.Image;
import UserModel, { UserAttr } from "../game_module/UserModel";
import { CfgManager } from "../GameLogic/CfgManager";
import NetManager, { PROTOCOLS } from "../game_module/NetManager";
import BannerAndVideo1 from "../game_module/BannerAndVideo1";
import { WXSdk } from "../game_module/WXSdk";
import MainRoleManager from "../Manager/MainRoleManager";
export class SKinShopView extends ui.SkinShopViewUI implements UI_Interface {
    private static instance: SKinShopView;
    static get Instance() {
        return SKinShopView.instance;
    }
    constructor() {
        super();
        SKinShopView.instance = this;
        this.resizeHander();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.resizeHander);
    }
    resizeHander() {
        this.height = Laya.stage.height;
    }
    OnShow(data?: any) {
        this.InitDate();
        this.InitHandler();
        this.InitUi();
        // DisplayerCarController.Instance.CreateModel(this.oignPlayerid, new Laya.Vector3(this.pos.x, this.pos.y, -10));
        //MainUi.Instance.ShowSKinShopView(false);
        // DisplayerCarController.Instance.InitDis(this.viewPort);
        WXSdk.AldEvent("打开皮肤界面");
    }
    private oignPlayerid: number;
    private operateIndex: number;
    InitDate() {
        this.oignPlayerid = PlayerManager.Instance.equipId;
        this.operateIndex = this.oignPlayerid;
    }
    InitUi(data?: any) {
        this.SetShowbtn(this.operateIndex);
        this.SetUseBtn(this.operateIndex);
        DisplayerCarController.Instance.InitDis(this.viewPort);
    }
    InitHandler() {
        this.backBtn.CZ = () => {
            //DisplayerCarController.Instance.CreateModel(1);
            this.BackHandler();
        };
        this.leftBtn.clickHandler = Laya.Handler.create(this, () => {
            if (this.operateIndex == 1) return;
            this.operateIndex--;
            this.SetShowbtn(this.operateIndex);
            this.SetUseBtn(this.operateIndex);
            DisplayerCarController.Instance.CreatePlayer(this.operateIndex);

        }, null, false);
        this.rightBtn.clickHandler = Laya.Handler.create(this, () => {
            if (this.operateIndex == 7) return;
            this.operateIndex++;
            this.SetShowbtn(this.operateIndex);
            this.SetUseBtn(this.operateIndex);
            DisplayerCarController.Instance.CreatePlayer(this.operateIndex);
        }, null, false);
        this.use.CZ = () => {
            this.NetEquip();
        };
        this.coin.CZ = () => {
            if (UserModel.Instance.CurModel.GetUserAttr(UserAttr.money) < this.coinValue)ViewManager.Instance.ShowTips("金币不足!",2)
            else
                this.NetBuy();
        };
        this.video.CZ = () => {
            this.VideoBtnHandler();
        };
    }

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

    private coinValue: number;
    SetUseBtn(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 = CfgManager.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;
        }
    }
    VideoBtnHandler() {
        BannerAndVideo1.ShowVideo((done) => {
            if (done) {
                WXSdk.AldEvent("商店中看视屏获得皮肤");
                this.NetVideo();
            }
        })
    };
    NetVideo() {
        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.SetUseBtn(this.operateIndex);
            },
            (data) => {

            });
    }
    NetBuy() {
        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.SetUseBtn(this.operateIndex);
            },
            (data) => {

            });
    }
    NetEquip() {
        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.SetUseBtn(this.operateIndex);
            },
            (data) => {

            });
    }



    BackHandler() {
        // MainUi.Instance.ShowSKinShopView(true);
         DisplayerCarController.Instance.Reset();
        if (this.oignPlayerid != PlayerManager.Instance.equipId) {
             MainRoleManager.Instance.ChangePlayer();
        }
        ViewManager.Instance.CloseView(ViewType.SKinShopView);
    }

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

}