namespace ScrewsMaster
{
    using DontConfuse;
    using FairyGUI;
    using FGUI.G006_menu;

    public class ProfileUI : BaseUI
    {
        private ProfileUICtrl ctrl;
        private ProfileModel model;
        private FGUI.G006_menu.com_edit ui;
        private LoginModel loginModel;

        private int selectIndex = -1;

        private int avatarTotal;

        public ProfileUI(ProfileUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.ProfileUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "G006_menu";
            uiInfo.assetName = "com_edit";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;
        }

        protected override void OnInit()
        {
            model = moduleManager.GetModel(ModelConst.ProfileModel) as ProfileModel;
            loginModel = moduleManager.GetModel(ModelConst.LoginModel) as LoginModel;
        }

        protected override void OnClose()
        {
            SdkManager.Instance.SetDarkThough(true);
            CommonHelper.FadeOut(ui);
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G006_menu.com_edit;
        }

        protected override void OnOpenBefore(object args)
        {
            SdkManager.Instance.SetDarkThough(false);
            InitData();
            InitView();
        }

        protected override void OnOpen(object args)
        {
            CommonHelper.FadeIn(ui);
        }

        private void InitData()
        {
            selectIndex = PreferencesMgr.Instance.PlayerAvatarId;
            avatarTotal = 12;
        }

        private void InitView()
        {
            ui.closeButton.onClick.Set(OnCloseView);

            GameHelper.SetName(ui.textp_name);

            ui.btn_ok.onClick.Set(OnClickOK);

            ui.list_avatar.itemRenderer = ItemRenderer;
            ui.list_avatar.numItems = avatarTotal;
        }

        private void OnClickOK()
        {
            var name = ui.textp_name.text;
            OnCloseView();

            if (selectIndex != -1)
            {
                PreferencesMgr.Instance.PlayerAvatarId = selectIndex;
            }

            if (string.IsNullOrEmpty(name) || name.IsNullOrWhiteSpace())
            {
                GameHelper.ShowTips("The input cannot be null", true);
                return;
            }

            if (!name.Equals(PreferencesMgr.Instance.PlayerName))
            {
                PreferencesMgr.Instance.PlayerName = name;
            }
        }

        private void ItemRenderer(int index, GObject item)
        {
            var btnImg = item as btn_img;
            var currentIndex = index + 1;

            TextureHelper.SetAvatarToLoader(currentIndex, btnImg.head_loader.GetChild("loader_avatar") as GLoader);
            btnImg.cont_select.selectedIndex = selectIndex == currentIndex
                ? btn_img.Select_select
                : btn_img.Select_none;

            btnImg.SetClick(() =>
            {
                selectIndex = currentIndex;
                ui.list_avatar.numItems = avatarTotal;
            });
        }

        private void OnCloseView()
        {
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.ProfileUI_Close);
        }
    }
}