using QuestValidator.Common.Helpers;
using QuestValidator.Models;
using QuestValidator.Models.Other;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;

namespace AssortGenerator.Common.Helpers
{
    public static class QuestHelper
    {
        // Quest to lookup is key, previous quest is PreceedingQuest value

        private static QuestRoot _liveQuestData;
        private static Dictionary<string, QuestValidator.Models.Quest> _questData;
        private static List<AssortUnlocks> _assortUnlocks;

        public static Dictionary<string, QuestValidator.Models.Quest> GetQuestData(string filename = "quests")
        {
            if (_questData is null)
            {
                var questFilePath = InputFileHelper.GetInputFilePaths().FirstOrDefault(x => x.Contains(filename));
                if (questFilePath is null)
                {
                    return null;
                }

                var questDataJson = File.ReadAllText(questFilePath);
                _questData = JsonSerializer.Deserialize<Dictionary<string, QuestValidator.Models.Quest>>(questDataJson);
            }

            return _questData;
        }

        public static QuestRoot GetLiveQuestData(string filename = "resp.client.quest.list")
        {
            if (_liveQuestData is null)
            {
                var questFilePath = InputFileHelper.GetInputFilePaths().FirstOrDefault(x => x.Contains(filename));
                if (questFilePath is null)
                {
                    return null;
                }

                var questDataJson = File.ReadAllText(questFilePath);
                _liveQuestData = JsonSerializer.Deserialize<QuestRoot>(questDataJson);
            }

            return _liveQuestData;
        }

        public static string GetQuestNameById(string id)
        {
            return QuestNames.GetNameById(id);
        }

        public static List<AssortUnlocks> GetAssortUnlocks()
        {
            if (_assortUnlocks is null)
            {
                _assortUnlocks = new List<AssortUnlocks>();
                foreach (var quest in GetQuestData())
                {
                    foreach (var reward in quest.Value.rewards.Success)
                    {
                        if (string.Equals(reward.type, "assortmentunlock", System.StringComparison.OrdinalIgnoreCase))
                        {
                            _assortUnlocks.Add(new AssortUnlocks
                            {
                                AssortUnlockId = reward.id,
                                ItemUnlockedId = reward.target,
                                ItemUnlockedTemplateId = reward.items[0]._tpl,
                                LoyaltyLevel = (int)reward.loyaltyLevel,
                                QuestId = quest.Key,
                                QuestRewardId = reward.id,
                                TraderId = reward.traderId,
                                TraderType = TraderHelper.GetTraderTypeById(reward.traderId),
                                Criteria = "Success"
                            }
                        );
                        }
                    }
                }
            }

            return _assortUnlocks;
        }

        public static bool DoesIconExist(string iconPath, string imageId)
        {
            var filesInPath = Directory.GetFiles(iconPath);
            return filesInPath.Any(x => x.Contains(imageId));
        }
    }
}