using System.Collections.Generic;
using System.Linq;

namespace ET.Server
{
    [FriendOf(typeof (PlayerSkillComponent))]
    public static class PlayerSkillComponentSystem
    {
        public class PlayerSkillComponentAwakeSystem: AwakeSystem<PlayerSkillComponent>
        {
            protected override void Awake(PlayerSkillComponent self)
            {
                Log.Info($"创建玩家技能组件...");
                WNPlayer player = self.GetParent<WNPlayer>();

                self.Data = new PlayerSkillInfo();
                self.Data.Skills = new List<GameStruct.PlayerSkillBaseData>();
                // 初始化技能数据
                CharacterConfig config = CharacterConfigCategory.Instance.Get(player.GetPro());
                if (config != null && config.InitSkillList.Count > 0)
                {
                    foreach (GameStruct.IntIntData intIntData in config.InitSkillList.Where(intIntData => intIntData != null))
                    {
                        self.Data.Skills.Add(new GameStruct.PlayerSkillBaseData(intIntData.value1, intIntData.value2, true, 0L));
                    }
                }
                // 初始化战斗服缓存数据
                self.ToJson4BattleServerSkills = self.GetBattleServerSkills();
            }
        }

        public class PlayerSkillComponentDestroySystem: DestroySystem<PlayerSkillComponent>
        {
            protected override void Destroy(PlayerSkillComponent self)
            {
            }
        }

        /// <summary>
        /// 被动技能, 发给bs
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        private static IEnumerable<GameStruct.SkillInfo> GetPassiveSkillInfoBS(this PlayerSkillComponent self)
        {
            List<GameStruct.SkillInfo> list = new List<GameStruct.SkillInfo>();

            foreach (SkillConfig prop in SkillConfigCategory.Instance.GetAll().Values.Where(prop => prop != null))
            {
                if (prop.ProId != self.GetParent<WNPlayer>().GetPro() || prop.SkillType != (int)SkillType.PLAYER_PASSIVE)
                {
                    continue;
                }

                GameStruct.SkillInfo info = new ();
                info.type = prop.SkillType;
                info.id = prop.Id;
                info.level = 1;
                info.skillTime = 0L;
                info.cdTime = 0;
                info.flag = 0;
                info.autoLaunch = false;

                list.Add(info);
            }

            return list;
        }

        /// <summary>
        /// 战斗服数据
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static List<GameStruct.SkillInfo> GetBattleServerSkills(this PlayerSkillComponent self)
        {
            List<GameStruct.SkillInfo> skills = new List<GameStruct.SkillInfo>();
            foreach (GameStruct.PlayerSkillBaseData skill in self.Data.Skills.Where(skill => skill != null))
            {
                if (!skill.unlock)
                {
                    continue;
                }

                SkillConfig prop = SkillConfigCategory.Instance.Get(skill.id);
                if (prop != null && !SkillHelper.IsPassive(prop))
                {
                    skills.Add(SkillHelper.NewSkillInfo(skill));
                }
            }

            // 被动技能战斗服信息
            skills.AddRange(self.GetPassiveSkillInfoBS());

            return skills;
        }
    }
}