using UnityEngine;
using FairyGUI;
using FGUI.G601_Coin;
using System;
using System.Text;
using DG.Tweening;

namespace ScrewsMaster
{
    public class CoinWnd_newUI : BaseUI
    {
        private CoinWnd_newUICtrl ctrl;
        private CoinWnd_newModel model;
        private FGUI.G601_Coin.Com_CoinWnd_new ui;

        private reward_data pos_arr;

        private bool isUpdate = false;




        private int completeSum = 0;




        private float toSizeAniTime = 0.4f;




        private float moveAniTime = 0.5f;




        private float showAniTime = 0.1f;




        private bool isRiseUI;

        private Action CloseCb;
        public CoinWnd_newUI(CoinWnd_newUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.CoinWnd_newUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "G601_Coin";
            uiInfo.assetName = "Com_CoinWnd_new";
            uiInfo.layerType = UILayerType.Animation;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = false;
        }
        public override void OnUpdate()
        {
            if (!isUpdate)
            {
                if (isRiseUI)
                {
                    // if (data.rewardSingleData.id == 101 || data.rewardSingleData.id == 111)
                    // {

                    GameHelper.OnRiseUIRecover(pos_arr.type, UILayerType.Normal);
                    isRiseUI = false;


                    //}
                }

                return;
            }

            //data?.Update();
        }
        #region 生命周期
        protected override void OnInit()
        {
            //model = ModuleManager.Instance.GetModel(ModelConst.CoinWnd_newModel) as CoinWnd_newModel;
        }

        protected override void OnClose()
        {
            // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.MaskUI_Close);//2024年9月10日检查发现动画会调用全屏遮罩影响游戏体验，故而关闭
            CloseCb?.Invoke();
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G601_Coin.Com_CoinWnd_new;
        }

        protected override void OnOpenBefore(object args)
        {
            if (AudioManager.Instance.IsOpenEffect)
            {
                AudioManager.Instance.PlayDynamicEffect(AudioConst.CoinFly);
            }
            pos_arr = (reward_data)args;
            // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.MaskUI_Open);//2024年9月10日检查发现动画会调用全屏遮罩影响游戏体验，故而关闭
            
            // if (args is RewardDisplayData displayModel)
            // {
            //     data = displayModel;
            //     if (data.isPlayAudio)
            //     {
            //         AudioManager.Instance.PlayDynamicEffect(data.audioName);
            //     }
            // }

            if (true)
            {
                // if (!data.isSingle)
                // {
                ShowText();
                // }


                // ui.com_Coins.cont_coin.selectedIndex = !data.isSingle ? com_Coins.Coin_more : com_Coins.Coin_single;
                // ui.com_Coins.position = data.rewardSingleData.startPosition;
                ui.com_Coins.cont_coin.selectedIndex = com_Coins.Coin_more;
                ui.com_Coins.position = pos_arr.start;

                float val = 0f;

                ui.fx_enter.position = ui.com_Coins.position;


                foreach (var o in ui.com_Coins.GetChildren())
                {
                    var item = (com_Coin)o;
                    if (!item.visible)
                    {
                        continue;
                    }

                    item.visible = false;
                    TextureHelper.GetItemIcon(pos_arr.type, spr => { item.icon.texture = spr; });
                    float def = val;
                    DOVirtual.DelayedCall(0.05f, () =>
                    {
                        item.visible = true;

                        Vector2 tar = new Vector2(UnityEngine.Random.Range(0, item.parent.size.x / 1),
                            UnityEngine.Random.Range(0, item.parent.size.y / 1));
                        item.size = Vector2.zero;
                        item.position = new Vector2(item.parent.width / 2, item.parent.height / 2);
                        // if (!data.isSingle)
                        // {
                        // }
                        // else
                        // {
                        //     toSizeAniTime = 0.2f;
                        // }

                        item.TweenMove(tar, toSizeAniTime + def);
                        DOTween.To(() => item.size, (e) => item.size = e, Vector2.one, toSizeAniTime + def);
                        item.icon.visible = true;
                        DOVirtual.DelayedCall(toSizeAniTime + def, () =>
                        {
                            var moveTime = moveAniTime;
                            DOTween.To(() => item.icon.alpha, (e) => { item.icon.alpha = e; }, 0.6f, moveTime);

                            item.TweenMove(pos_arr.end - (Vector2)item.parent.position +
                                           item.parent.size / 2, moveTime);

                            DOVirtual.DelayedCall(moveTime, () =>
                            {
                                item.icon.visible = false;
                                completeSum++;
                                var fxPlayData = FxPlayData.Get(Fx_Type.fx_ui_jinbi_click, 1,
                                    item.LocalToRoot(Vector2.zero, GRoot.inst));
                                uiCtrlDispatcher.Dispatch(UICtrlMsg.PlayUIFX, fxPlayData);

                                if (!isUpdate)
                                {
                                    isUpdate = true;



                                }

                                if (completeSum >= ui.com_Coins.numChildren)
                                {
                                    DOVirtual.DelayedCall(0.2f, () => { CtrlCloseUI(); });
                                }
                            });
                        });
                    });
                    val += 0.02f;
                }
            }
        }
        private void ShowText()
        {

            var sb = new StringBuilder();
            sb.Append("+");
            // if (data.rewardSingleData.id == 101 || data.rewardSingleData.id == 111)
            // {

            // sb.Append((data.rewardSingleData.GetTotalValue() * GameHelper.GetPaymentMulti()).ToString("N"));
            sb.Append(pos_arr.change);
            //}
            // else
            // {
            //     sb.Append(data.rewardSingleData.GetTotalValue().ToString("N0"));
            // }

            ui.text_Add.SetText(sb.ToString());
            ui.text_Add.position = pos_arr.start;
            ui.fx_addSum.Play();
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }
        #endregion

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion

        //初始化页面逻辑
        private void InitView()
        {

        }
    }
}
class reward_data
{
    public Vector2 start;
    public Vector2 end;
    public float change;
    public int type;
}