using AssortHelpers.Common.Helpers;
using AssortValidator.Common.Helpers.Models;
using AssortValidator.Models;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;

namespace AssortValidator.Common.Helpers
{
    public static class QuestHelper
    {
        private static readonly Dictionary<string, string> questNames = new Dictionary<string, string>
        {
            { "5936d90786f7742b1420ba5b", "Debut" },
            { "5936da9e86f7742d65037edf", "Checking"},
            { "59674cd986f7744ab26e32f2", "Shootout picnic"},
            { "59674eb386f774539f14813a", "Delivery from the past"},
            { "5967530a86f77462ba22226b", "Bad rep evidence"},
            { "59675d6c86f7740a842fc482", "Ice cream cones"},
            { "59675ea386f77414b32bded2", "Postman Pat"},
            { "596760e186f7741e11214d58", "Postman Pat, p. 2"},
            { "5967725e86f774601a446662", "Shaking up teller"},
            { "5967733e86f774602332fc84", "Shortage"},
            { "59689ee586f7740d1570bbd5", "Sanitary Standards, p. 1"},
            { "59689fbd86f7740d137ebfc4", "Operation Aquarius"},
            { "5968eb3186f7741dde183a4d", "Operation Aquarius, p. 2"},
            { "5969f90786f77420d2328015", "Painkiller"},
            { "5969f9e986f7741dde183a50", "Pharmacist"},
            { "596a0e1686f7741ddf17dbee", "Supply plans"},
            { "596a101f86f7741ddb481582", "Kind of sabotage" },
            { "596a1e6c86f7741ddc2d3206", "General wares"},
            { "596a204686f774576d4c95de", "Sanitary Standards, p. 2"},
            { "596a218586f77420d232807c", "Car repair"},
            { "596b36c586f77450d6045ad2", "Supplier"},
            { "596b43fb86f77457ca186186", "The Extortionist"},
            { "596b455186f77457cb50eccb", "Stirrup"},
            { "5979ed3886f77431307dc512", "What’s on the flash drive?"},
            { "5979eee086f774311955e614", "Golden swag"},
            { "5979f8bb86f7743ec214c7a6", "Polikhim hobo"},
            { "5979f9ba86f7740f6c3fe9f2", "Chemical, p.1"},
            { "597a0b2986f77426d66c0633", "Chemical, p. 2"},
            { "597a0e5786f77426d66c0636", "Chemical, p. 3"},
            { "597a0f5686f774273b74f676", "Chemical, p. 4"},
            { "597a160786f77477531d39d2", "Out of curiosity"},
            { "597a171586f77405ba6887d3", "Big customer"},
            { "59c124d686f774189b3c843f", "BP depot"},
            { "59c50a9e86f7745fef66f4ff", "The Punisher"},
            { "59c50c8886f7745fed3193bf", "The Punisher. Part 2."},
            { "59c512ad86f7741f0d09de9b", "The Punisher. Part 3."},
            { "59c9392986f7742f6923add2", "Trust regain"},
            { "59c93e8e86f7742a406989c4", "Loyalty buyout"},
            { "59ca1a6286f774509a270942", "No offence"},
            { "59ca264786f77445a80ed044", "The Punisher. Part 4."},
            { "59ca29fb86f77445ab465c87", "The Punisher. Part 5."},
            { "59ca2eb686f77445a80ed049", "The Punisher. Part 6."},
            { "59f9da6786f774714230d751", ""},
            { "5a03153686f77442d90e2171", "Spa Tour. Part 1"},
            { "5a03173786f77451cb427172", "Spa Tour. Part 2"},
            { "5a0327ba86f77456b9154236", "Spa Tour. Part 3" },
            { "5a03296886f774569778596a", "Spa Tour. Part 4"},
            { "5a0449d586f77474e66227b7", "Spa Tour. Part 5"},
            { "5a27b75b86f7742e97191958", "Fishing Gear"},
            { "5a27b7a786f774579c3eb376", "Tigr Safari"},
            { "5a27b7d686f77460d847e6a6", " Scrap Metal"},
            { "5a27b80086f774429a5d7e20", "Eagle Eye"},
            { "5a27b87686f77460de0252a8", "Humanitarian Supplies"},
            { "5a27b9de86f77464e5044585", "The Cult. Part 1"},
            { "5a27ba1c86f77461ea5a3c56", "The Cult. Part 2"},
            { "5a27ba9586f7741b543d8e85", "Spa Tour. Part 6"},
            { "5a27bafb86f7741c73584017", "Spa Tour. Part 7"},
            { "5a27bb1e86f7741f27621b7e", "Cargo X. Part 1"},
            { "5a27bb3d86f77411ea361a21", "Cargo X. Part 2"},
            { "5a27bb5986f7741dfb660900", "Cargo X. Part 3"},
            { "5a27bb8386f7741c770d2d0a", "Wet Job. Part 1"},
            { "5a27bbf886f774333a418eeb", "Wet Job. Part 2"},
            { "5a27bc1586f7741f6d40fa2f", "Wet Job. Part 3"},
            { "5a27bc3686f7741c73584026", "Wet Job. Part 4"},
            { "5a27bc6986f7741c7358402b", "Wet Job. Part 5"},
            { "5a27bc8586f7741b543d8ea4", "Wet Job. Part 6"},
            { "5a27c99a86f7747d2c6bdd8e", "Friend from the West. Part 1"},
            { "5a27d2af86f7744e1115b323", "Friend from the West. Part 2"},
            { "5a5642ce86f77445c63c3419", "Hippocratic Vow"},
            { "5a68661a86f774500f48afb0", "Health Care Privacy. Part 1"},
            { "5a68663e86f774501078f78a", "Health Care Privacy. Part 2"},
            { "5a68665c86f774255929b4c7", "Health Care Privacy. Part 3"},
            { "5a68667486f7742607157d28", "Health Care Privacy. Part 4"},
            { "5a68669a86f774255929b4d4", "Health Care Privacy. Part 5"},
            { "5ac23c6186f7741247042bad", "Gunsmith. Part 1"},
            { "5ac2426c86f774138762edfe", "Gunsmith. Part 2"},
            { "5ac2428686f77412450b42bf", "Gunsmith. Part 3"},
            { "5ac242ab86f77412464f68b4", "Gunsmith. Part 5"},
            { "5ac244c486f77413e12cf945", "Gunsmith. Part 6"},
            { "5ac244eb86f7741356335af1", "Gunsmith. Part 4"},
            { "5ac345dc86f774288030817f", "Farming. Part 1" },
            { "5ac3460c86f7742880308185", "Farming. Part 2" },
            { "5ac3462b86f7741d6118b983", "Farming. Part 3" },
            { "5ac3464c86f7741d651d6877", "Farming. Part 4" },
            { "5ac3467986f7741d6224abc2", "Signal. Part 1"  },
            { "5ac346a886f7744e1b083d67", "Signal. Part 2"  },
            { "5ac346cf86f7741d63233a02", "Signal. Part 3"  },
            { "5ac346e886f7741d6118b99b", "Signal. Part 4"  },
            { "5ac3475486f7741d6224abd3", "Bad habit"                    },
            { "5ac3477486f7741d651d6885", "Scout"                        },
            { "5ac3479086f7742880308199", "Insider"                      },
            { "5ae3267986f7742a413592fe", "Gunsmith. Part 7"             },
            { "5ae3270f86f77445ba41d4dd", "Gunsmith. Part 8"             },
            { "5ae3277186f7745973054106", "Gunsmith. Part 9"             },
            { "5ae327c886f7745c7b3f2f3f", "Gunsmith. Part 10"            },
            { "5ae3280386f7742a41359364", "Gunsmith. Part 11"            },
            { "5ae448a386f7744d3730fff0", "Only business"                },
            { "5ae448bf86f7744d733e55ee", "Make ULTRA Great Again"       },
            { "5ae448e586f7744dcf0c2a67", "Big sale"                     },
            { "5ae448f286f77448d73c0131", "The Blood of War"             },
            { "5ae4490786f7744ca822adcc", "Dressed to kill"              },
            { "5ae4493486f7744efa289417", "Database. Part 1"             },
            { "5ae4493d86f7744b8e15aa8f", "Database. Part 2"             },
            { "5ae4495086f77443c122bc40", "Sew it good. Part 1"          },
            { "5ae4495c86f7744e87761355", "Sew it good. Part 2"          },
            { "5ae4496986f774459e77beb6", "Sew it good. Part 3"          },
            { "5ae4497b86f7744cf402ed00", "Sew it good. Part 4"          },
            { "5ae4498786f7744bde357695", "The key to success"           },
            { "5ae4499a86f77449783815db", "Charisma brings success"      },
            { "5ae449a586f7744bde357696", "No fuss needed"               },
            { "5ae449b386f77446d8741719", "Gratitude"                    },
            { "5ae449c386f7744bde357697", "Sales Night"                  },
            { "5ae449d986f774453a54a7e1", "Supervisor"                   },
            { "5b47749f86f7746c5d6a5fd4", "Gunsmith. Part 12"            },
            { "5b47799d86f7746c5d6a5fd8", "Gunsmith. Part 13"            },
            { "5b477b6f86f7747290681823", "Gunsmith. Part 14"            },
            { "5b477f7686f7744d1b23c4d2", "Gunsmith. Part 15"            },
            { "5b47825886f77468074618d3", "Gunsmith. Part 16"            },
            { "5b47876e86f7744d1c353205", "The Blood of War Part 2"      },
            { "5b47891f86f7744d1b23c571", "Living high is not a crime"   },
            { "5b478b1886f7744d1b23c57d", "Hot delivery"                 },
            { "5b478d0f86f7744d190d91b5", "Minibus"                      },
            { "5b478eca86f7744642012254", "\"Vitamins\" p.1"             },
            { "5b478ff486f7744d184ecbbf", "\"Vitamins\" p. 2"            },
            { "5b47926a86f7747ccc057c15", "Informed means armed"         },
            { "5b4794cb86f774598100d5d4", "Lend lease"                   },
            { "5b4795fb86f7745876267770", "Chumming"                     },
            { "5bc4776586f774512d07cf05", "\"The Tarkov shooter\" Part 1"},
            { "5bc479e586f7747f376c7da3", "\"The Tarkov shooter\" Part 2"},
            { "5bc47dbf86f7741ee74e93b9", "\"The Tarkov shooter\". Part 3"   },
            { "5bc480a686f7741af0342e29", "\"The Tarkov shooter\". Part 4"   },
            { "5bc4826c86f774106d22d88b", "\"The Tarkov shooter\". Part 5"   },
            { "5bc4836986f7740c0152911c", "\"The Tarkov shooter\". Part 6"   },
            { "5bc4856986f77454c317bea7", "\"The Tarkov shooter\". Part 7"   },
            { "5bc4893c86f774626f5ebf3e", "\"The Tarkov shooter\". Part 8"   },
            { "5c0bbaa886f7746941031d82", "Bullshit"                         },
            { "5c0bc91486f7746ab41857a2", "Silent caliber"                   },
            { "5c0bd01e86f7747cdd799e56", "Insomnia"                         },
            { "5c0bd94186f7747a727f09b2", "Test drive. Pt. 1"                },
            { "5c0bdb5286f774166e38eed4", "Flint"                            },
            { "5c0bde0986f77479cf22c2f8", "A Shooter Born in Heaven"         },
            { "5c0be13186f7746f016734aa", "Psycho Sniper"                    },
            { "5c0be5fc86f774467a116593", "Private clinic"                   },
            { "5c0d0d5086f774363760aef2", "Athlete"                          },
            { "5c0d0f1886f77457b8210226", "Lend lease. Part 2"               },
            { "5c0d190cd09282029f5390d8", "Grenadier"                        },
            { "5c0d1c4cd0928202a02a6f5c", "Decontamination service"          },
            { "5c0d4c12d09282029f539173", "Peacekeeping mission"             },
            { "5c0d4e61d09282029f53920e", "The guide"                        },
            { "5c10f94386f774227172c572", "The Blood of War Pt. 3"           },
            { "5c1128e386f7746565181106", "Fertilizers"                      },
            { "5c112d7e86f7740d6f647486", "Scavenger"                        },
            { "5c1141f386f77430ff393792", "Living high is not a crime Pt.2"  },
            { "5c1234c286f77406fa13baeb", "Setup"                            },
            { "5c12452c86f7744b83469073", "Perfect mediator"                 },
            { "5c139eb686f7747878361a6f", "Import"                           },
            { "5c51aac186f77432ea65c552", "Collector"                        },
            { "5d2495a886f77425cd51e403", "Introduction"                     },
            { "5d24b81486f77439c92d6ba8", "Acquaintance"                     },
            { "5d25aed386f77442734d25d2", "The survivalist path. Unprotected, but dangerous"},
            { "5d25b6be86f77444001e1b89", "The survivalist path. Thrifty"                  },
            { "5d25bfd086f77442734d3007", "The survivalist path. Zhivchik"                 },
            { "5d25c81b86f77443e625dd71", "The survivalist path. Wounded beast"            },
            { "5d25cf2686f77443e75488d4", "The survivalist path. Tough guy"                },
            { "5d25d2c186f77443e35162e5", "The survivalist path. Cold blooded"             },
            { "5d25dae186f77443e55d2f78", "The survivalist path. Zatoichi"                 },
            { "5d25e29d86f7740a22516326", "The survivalist path. Eagle-owl"        },
            { "5d25e2a986f77409dd5cdf2a", "The survivalist path. Combat medic"     },
            { "5eaaaa7c93afa0558f3b5a1c", "The survivalist path. Junkie"           },
            { "5d25e2b486f77409de05bba0", "Huntsman path. Secured perimeter"       },
            { "5d25e2c386f77443e7549029", "Huntsman path. The trophy"              },
            { "5d25e2cc86f77443e47ae019", "Huntsman path. Woods cleaning"          },
            { "5d25e2d886f77442734d335e", "Huntsman path. Controller"              },
            { "5d25e2e286f77444001e2e48", "Huntsman path. Sell-out"                },
            { "5d25e2ee86f77443e35162ea", "Huntsman path. Woods keeper"            },
            { "5d25e43786f7740a212217fa", "Huntsman path. Justice"                 },
            { "5d25e44386f77409453bce7b", "Huntsman path. Evil watchman"           },
            { "5d25e44f86f77443e625e385", "Huntsman path. Eraser"                  },
            { "5d25e45e86f77408251c4bfa", "Huntsman path. Eraser pt. 2"            },
            { "5d25e46e86f77409453bce7c", "Ambulance"                              },
            { "5d25e48186f77443e625e386", "Courtesy visit"                         },
            { "5d25e48d86f77408251c4bfb", "Shady business"                         },
            { "5d25e4ad86f77443e625e387", "Nostalgia"                              },
            { "5d25e4b786f77408251c4bfc", "Fishing place"                          },
            { "5d25e4ca86f77409dd5cdf2c", "Hunting trip"                           },
            { "5d25e4d586f77443e625e388", "Reserv"                                 },
            { "5d4bec3486f7743cac246665", "Regulated materials"                            },
            { "5d6fb2c086f77449da599c24", "An apple a day - keeps the doctor away"         },
            { "5d6fbc2886f77449d825f9d3", "Mentor"                   },
            { "5dc53acb86f77469c740c893", "The stylish one"          },
            { "5e381b0286f77420e3417a74", "Textile. Part 1"          },
            { "5e4d515e86f77438b2195244", "Textile. Part 2"          },
            { "5ede55112c95834b583f052a", "Bunker. Part 1"           },
            { "5ede567cfa6dc072ce15d6e3", "Bunker. Part 2"           },
            { "5eda19f0edce541157209cee", "Anesthesia"               },
            { "5edab4b1218d181e29451435", "Huntsman path. Sadist"    },
            { "5edab736cc183c769d778bc2", "Colleagues. Pt.1"         },
            { "5edaba7c0c502106f869bc02", "Colleagues. Pt.2"         },
            { "5edac34d0bb72a50635c2bfa", "Colleagues. Pt.3"         },
            { "5edabd13218d181e29451442", "Rigged game"              },
            { "5edac020218d181e29451446", "Samples"                  },
            { "5edac63b930f5454f51e128b", "TerraGroup employee"      },
            { "5f04886a3937dc337a6b8238", "The chemistry closet"     },
            { "5fd9fad9c1ce6b1a3b486d00", "Search Mission"},
            { "600302d73b897b11364cd161", "Hunter"},
            { "6086c852c945025d41566124", "Revision"},
            { "608a768d82e40b3c727fd17d", "Pest control"},
            { "6089736efa70fc097863b8f6", "Back door"},
            { "6089743983426423753cd58a", "Safe corridor"},
            { "608974af4b05530f55550c21", "Inventory check"},
            { "608974d01a66564e74191fc0", "Fuel matter"},
            { "60896bca6ee58f38c417d4f2", "No place for renegades"},
            { "60896e28e4a85c72ef3fa301", "Disease history"},
            { "60896b7bfa70fc097863b8f5", "Documents"},
            { "6089732b59b92115597ad789", "Surplus goods"},
            { "60896888e4a85c72ef3fa300", "Experience exchange"},
            { "60c0c018f7afb4354815096a", "Tagilla"},
            { "60e71dc0a94be721b065bbfc", "Intimidator" },
            { "60e71dc67fcf9c556f325056", "level 55 quest" },
            { "60e71e8ed54b755a3b53eb67", "Huntsman Path - Relentless" },
            { "60e729cf5698ee7b05057439", "Swift One" },
            { "60effd818b669d08a35bfad5", "The Choice" },
            { "60e71c11d54b755a3b53eb65", "Night Sweep" },
            { "60e71bb4e456d449cd47ca75", "Intimidator"},
            { "60e71c48c1bfa3050473b8e5", "Crisis"},
            { "60e71b9bbd90872cb85440f3", "Capturing Outposts"},
            { "60e71b62a0beca400d69efc4", "Escort"},
            { "60e71d23c1bfa3050473b8e6", "Calibration"},
            { "60e71c9ad54b755a3b53eb66", "Mutual Interest"},
            { "6179ac7511973d018217d0b9", "Easy Job Part 1" },
            { "6179acbdc760af5ad2053585", "Easy Job Part 2" },
            { "6179b3a12153c15e937d52bc", "Energy Crisis" },
            { "6179ad74bca27a099552e03a", "Communication Difficulties" },
            { "6179aff8f57fb279792c60a1", "Overpopulation" },
            { "6179ad56c760af5ad2053587", "Seaside Vacation" },
            { "6179b3bdc7560e13d23eeb8d", "Corporate Secrets" },
            { "6179b5eabca27a099552e052", "Counteraction" },
            { "6179ad0a6e9dd54ac275e3f2", "Huntsman Path Outcasts" },
            { "6179b4f16e9dd54ac275e407", "MissingCargo" },
            { "6179b4d1bca27a099552e04e", "Revision Part 2" },
            { "6179b5b06e9dd54ac275e409", "Our Own Land" },
            { "61904daa7d0d857927447b9c", "The Hermit" },
            { "6193850f60b34236ee0483de", "Long Road" },
            { "61958c366726521dd96828ec", "Cargo X Part 4" },
            { "6179afd0bca27a099552e040", "Lost Contact"},
            { "613708a7f8333a5d15594368", "Action Test"}
        };
        private static QuestRoot _liveQuestData;

        public static QuestRoot GetLiveQuestData(string filename = "resp.client.quest.list")
        {
            if (_liveQuestData == null)
            {
                var questFilePath = InputFileHelper.GetInputFilePaths().FirstOrDefault(x => x.Contains(filename));
                var questDataJson = File.ReadAllText(questFilePath);
                _liveQuestData = JsonSerializer.Deserialize<QuestRoot>(questDataJson);
            }

            return _liveQuestData;
        }

        public static List<QuestAssortUnlock> GetQuestAssortUnlocks(List<Quest> quests)
        {
            var results = new List<QuestAssortUnlock>();

            foreach (var quest in quests)
            {
                foreach (var assortUnlockReward in quest.rewards.Success.Where(x=>x.type == "AssortmentUnlock"))
                {
                    results.Add(new QuestAssortUnlock{
                        QuestId = quest._id,
                        QuestName = QuestHelper.GetQuestNameById(quest._id),
                        TraderId = assortUnlockReward.traderId,
                        TraderType = TraderHelper.GetTraderTypeById(assortUnlockReward.traderId),
                        AssortId = assortUnlockReward.items[0]._id,
                        AssortTemplateId = assortUnlockReward.items[0]._tpl,
                        LoyaltyLevel = assortUnlockReward.loyaltyLevel.GetValueOrDefault(0) 
                    });
                }
            }


            return results;
        }

        private static string GetQuestNameById(string id)
        {
            if (questNames.ContainsKey(id))
            {
                return questNames[id];
            }

            return "Unknown Quest";
            
        }
    }
}