using FairyGUI;
using System.Linq;
using UnityEngine;

namespace ChillConnect
{
    using System;
    using System.Collections.Generic;

    [Flags]
    public enum RewardDisplayType
    {
        None = 0b_0000_0000,


        Number = 0b_0000_0001,


        ValueChange = 0b_0000_0010,


        Dialog = 0b_0000_0100,


        RewardFly = 0b_0000_1000,
    }


    public enum RewardOrigin
    {
        None,
        GameWin,
        H5Icon,
        LuckyWheel,
        SignIn,
        AdTask,
        Play,
        Achievement,
        H5Fly101,
        H5Fly102,
        Passview,
    }

    public enum RewardCondition
    {
        None,
        AD,
    }


    public class RewardSingleData
    {
        public int id;
        public int type;
        public decimal value;
        public decimal multiRate;
        public decimal rate;

        public decimal GetTotalValue()
        {
            return Math.Round(value * rate, 2);
        }

        public RewardOrigin origin;
        public Vector2 startPosition;
        public Vector2 endPosition;
        public int index;
        public RewardSingleData(int id, decimal value, RewardOrigin origin, int index = 0)
        {
            this.id = id;
            this.value = value;
            this.origin = origin;
            this.index = index;
            InitData();
        }

        private void InitData()
        {
            InitRate();
            InitMultiRate();
        }

        private void InitRate()
        {
            if (rate == 0)
            {
                rate = 1;
            }
        }

        private void InitMultiRate()
        {
            if (multiRate == 0)
            {
                multiRate = 1;
            }
        }

        public void InitFlyPosition()
        {
            InitStartPosition();
            InitEndPosition();
        }

        private void InitStartPosition()
        {
            if (startPosition == Vector2.zero)
            {
                startPosition = GRoot.inst.size / 2;
            }
        }

        private void InitEndPosition()
        {
            if (endPosition == Vector2.zero && id != 0)
            {
                var itemUI = GameHelper.GetItemUI(id);
                var gObject = itemUI?.GetChildAt(1);
                if (gObject != null)
                {
                    endPosition = itemUI.LocalToRoot((Vector2)gObject.position + gObject.size / 2, GRoot.inst);
                }
            }
        }

        public bool IsCanFly()
        {
            return IsCanFly(id);
        }

        public static bool IsCanFly(int id)
        {
            return id == 101 || id == 102 || id == 111 || id == 112;
        }
    }


    public class RewardData
    {
        private List<RewardSingleData> rewardDataList;

        public RewardDisplayType displayType;

        public RewardCondition condition;

        private Action<bool> mOnCompleted;

        public bool isSingle;
        public int sign_in_days;
        public decimal ctRate;

        public RewardData()
        {
            InitData();
        }

        public void AddCompleted(Action<bool> onCompleted)
        {
            mOnCompleted += onCompleted;
        }


        public void OnCompleted(bool isSuccess)
        {
            mOnCompleted?.Invoke(isSuccess);
        }

        public void AddReward(RewardSingleData rewardSingleData)
        {
            rewardDataList ??= new List<RewardSingleData>();

            var isNeedAdd = true;

            foreach (var data in rewardDataList.Where(data => data.id == rewardSingleData.id))
            {
                data.value += rewardSingleData.value;
                isNeedAdd = false;
                break;
            }

            if (isNeedAdd)
            {
                rewardDataList.Add(rewardSingleData);
            }
        }

        private void InitData()
        {
            if (rewardDataList != null)
            {
                rewardDataList.Clear();
            }
            else
            {
                rewardDataList = new List<RewardSingleData>();
            }
        }

        public List<RewardSingleData> GetRewardDataList()
        {
            return rewardDataList;
        }

        public int GetRewardFlyCount()
        {
            return GetRewardDataList().Count(rewardSingleData => rewardSingleData.IsCanFly());
        }
    }
}