/*
文件名:0_Login.nut
路径:User/UI/0_Login.nut
创建日期:2024-12-01	15:13
文件用途:登录界面窗口
*/
class _Login_Window extends Yosin_Window {
    //调试模式
    // DeBugMode = true;

    //不是窗口
    // NoWindow = true;

    //是否可见
    // Visible = false;

    AccountInputBox = null;
    PasswordInputBox = null;

    BackGroundMusic = null;

    //信息
    PackInfo = null;

    constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH) {
        base.constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH);

        //注册绘制
        RegisterDraw();
        //注册控件
        RegisterWidget();
        // OpenDeBug();

        //播放音乐
        PlayBackgroundMusic();

        //注册登录回调包
        MySocket.RegisterHandler(2, function(Jso) {
            //登录成功
            if (Jso.state) {
                _Yosin_MessageBox("登录成功,正在进入游戏...");
                MySocket.Send(9, null);
            } else {
                _Yosin_MessageBox("登录失败");
            }
        }.bindenv(this));

        //注册获取角色列表回包
        MySocket.RegisterHandler(10, function(Jso) {
            PackInfo = Jso;
        }.bindenv(this));
        //注册获取角色列表的装备回包
        MySocket.RegisterBinaryHandler(10, function(Blob) {
            Blob.readn('i');
            foreach(info in PackInfo.charac) {
                info.equip <- [];
                for (local i = 0; i< 15; i++) {
                    local value = Blob.readn('i');
                    info.equip.append(value);
                }
            }
            local Window = Sq_CreateWindow(_Select_Character_Window, "选择角色界面窗口", 0, 0, 1066, 600, 0);
            Window.Init(PackInfo);
        }.bindenv(this));
    }

    function PlayBackgroundMusic() {
        // BackGroundMusic = Sound("SoundPacks/Loop.ogg");
        // BackGroundMusic.Play();
    }

    function MusicLogic() {
        if (BackGroundMusic == null) return;
        if (!BackGroundMusic.IsPlaying()) BackGroundMusic.Play();
    }

    function RegisterWidget() {
        //账号输入框
        local AccountInputBox = Yosin_InputBox(752, 240, 200);
        AddUIChild(AccountInputBox);

        //密码输入框
        local PasswordInputBox = Yosin_InputBox(752, 280, 200);
        AddUIChild(PasswordInputBox);

        //登录按钮
        local LoginButton = Yosin_BaseButton(770, 410, 77, 24 "sprite/interface/lenheartwindowcommon.img", 90);
        //点击事件回调
        LoginButton.OnClick = function(Button) {
            MySocket.Send(1, {
                account = AccountInputBox.str,
                password = PasswordInputBox.str
            })
        }.bindenv(this);

        //登录按钮文本
        local LoginTextActor = FontAssetManager.GenerateNormal("登录", true, {
            color = sq_RGBA(200, 195, 169, 255)
        });
        LoginTextActor.SetPosition(26, 4);
        LoginButton.Addchild(LoginTextActor);
        AddUIChild(LoginButton);

        //注册按钮
        local RegisterButton = Yosin_BaseButton(865, 410, 77, 24 "sprite/interface/lenheartwindowcommon.img", 90);
        //点击事件回调
        RegisterButton.OnClick = function(Button) {
            MySocket.Send(3, {
                account = AccountInputBox.str,
                password = PasswordInputBox.str
            })
        }.bindenv(this);
        //注册按钮文本
        local RegisterTextActor = FontAssetManager.GenerateNormal("注册", true, {
            color = sq_RGBA(200, 195, 169, 255)
        });
        RegisterTextActor.SetPosition(26, 4);
        RegisterButton.Addchild(RegisterTextActor);
        AddUIChild(RegisterButton);
    }

    function RegisterDraw() {
        //大背景
        local BackGround = CL_SpriteObject("sprite/interface2/charactercreatever2/characterbackground.img", 14);
        Addchild(BackGround);
        //左上角的背景
        local Cover = CL_SpriteObject("sprite/interface2/channel/channelselect/channelbackground/channelbackground.img", 20);
        Addchild(Cover);
        //登录框的背景
        local LoginBoxBg = CL_SpriteObject("sprite/login_ui.img", 3);
        LoginBoxBg.SetPosition(634, 0);
        Addchild(LoginBoxBg);
        local AniObj = Animation("ui/selectcharacter/animation/selectcharactereffect.ani");
        AniObj.SetPosition(770, 0);
        Addchild(AniObj);
        //登录游戏字体
        local LoginText = CL_SpriteObject("sprite/login_ui.img", 0);
        LoginText.SetPosition(796, 65);
        Addchild(LoginText);
        //登录游戏说明
        local LoginTexttip = CL_SpriteObject("sprite/login_ui.img", 1);
        LoginTexttip.SetPosition(-33, 40);
        LoginText.Addchild(LoginTexttip);

        //账号
        local AccountTextActor = FontAssetManager.GenerateNormal("账号:", false, {
            color = sq_RGBA(200, 195, 169, 255)
        });
        AccountTextActor.SetPosition(720, 241);
        Addchild(AccountTextActor);
        //密码
        local PasswordTextActor = FontAssetManager.GenerateNormal("密码:", false, {
            color = sq_RGBA(200, 195, 169, 255)
        });
        PasswordTextActor.SetPosition(720, 281);
        Addchild(PasswordTextActor);
    }


    //逻辑入口
    function Proc(Dt) {
        MusicLogic();

        SyncPos(X, Y);
        base.Proc(Dt);
    }

}