using FairyGUI;
using FGUI.ZM_Common_01;
using FGUI.ZM_Setting_07;
using IgnoreOPS;

// using FGUI.G006_menu;

namespace RedHotRoast
{
    public class PersonViewUI : BaseUI
    {
        private PersonViewUICtrl ctrl;
        private PersonViewModel model;
        private int selectIndex = -1;

        private int total_item = 12;
        private FGUI.ZM_Setting_07.com_person ui;

        public PersonViewUI(PersonViewUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.MenuUI;
            this.ctrl = ctrl;
        }

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

        #region 生命周期

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

        protected override void OnClose()
        {
            CommonHelper.FadeOut(ui);
            UnityManager.ShezhiACT(true);

        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.ZM_Setting_07.com_person;
        }

        protected override void OnOpenBefore(object args)
        {
            // if (Screen.safeArea.y != 0)
            // {
            //     ui.title.y += Screen.safeArea.y;
            // }
            selectIndex = DataMgr.PlayerAvatarId.Value;
            UnityManager.ShezhiACT(false);
            InitView();



        }

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

        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }

        #endregion

        #region 消息

        protected override void AddListener()
        {
            HallManager.Instance.AddChangeGiftSwitch(InitView);
        }

        protected override void RemoveListener()
        {
            HallManager.Instance.RemoveChangeGiftSwitch(InitView);
        }

        #endregion

        private void InitView()
        {


            // var namStr = GameHelper.GetUserName();
            // ui.edit_name.input.text = namStr;
            ui.btn_update.SetClick(UpdateUserInfo);
            // ui.edit_name.btn_amend.SetClick(SaveName);
            ui.btn_close.SetClick(CtrlCloseUI);

            ui.list.itemRenderer = UpdateItem;
            ui.list.numItems = total_item;

        }

        private void UpdateItem(int index, GObject items)
        {
            var currentIndex = index + 1;
            var head = items as btn_item_head;
            var imgHead = head.head as head;

            TextureHelper.SetAvatarToLoader(currentIndex, imgHead.load_avatar);
            head.head_select.selectedIndex = selectIndex == currentIndex
                ? btn_item_head.Head_select_select
                : btn_item_head.Head_select_none;

            head.SetClick(() =>
            {
                selectIndex = currentIndex;
                ui.list.numItems = total_item;
            });


        }


        private void UpdateUserInfo()
        {
            if (selectIndex != -1)
            {
                DataMgr.PlayerAvatarId.Value = selectIndex;
            }

            // SaveName();

            CtrlCloseUI();
        }

        private void SaveName()
        {
            var name = ui.edit_name.input.text;

            if (string.IsNullOrEmpty(name) || name.IsNullOrWhiteSpace())
            {
                GameHelper.ShowTips("empty_input", true);
                return;
            }

            // if (name.Equals(GameHelper.GetPlayerInviteCode()))
            // {
            //     return;
            // }

            if (name.Equals(DataMgr.PlayerName.Value)) return;

            GameHelper.ShowTips($"save_name",true);
            DataMgr.PlayerName.Value = name;
        }

    }
}
