﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Codeplex.Data;
using jsonsTools;

namespace WotloggerModAPI
{

    public class WoTItems
    {

        public static  Dictionary<int, Vehicle> Vehicles { get; private set; }
        public static Dictionary<int, Arena> Arenas { get; private set; }
        public Dictionary<int, VehicleType> VehicleTypes { get { return _vehicleTypes; } }
        public Dictionary<int, Nation> Nations { get { return _nations; } }

        private static Dictionary<int, VehicleType> _vehicleTypes;
        private static Dictionary<int, Nation> _nations;

        public WoTItems(string modPath)
        {
            var jsonPath = Path.Combine(modPath, "game_items.json");
            Vehicles = new Dictionary<int, Vehicle>();
            Arenas = new Dictionary<int, Arena>();
            _vehicleTypes = new Dictionary<int, VehicleType>();
            _nations = new Dictionary<int, Nation>();

            dynamic items = jsonTools.DynamicFromFile(jsonPath);

            foreach (var k in items.vehicles.types)
            {
                var v = (string[])k.Value;
                int nr = Convert.ToInt32(k.Key);
                _vehicleTypes.Add(nr, new VehicleType(nr, v[0], v[1]));
            }



            foreach (var k in items.arenas.list)
            {
                var v = (string[])k.Value;
                int nr = Convert.ToInt32(k.Key);
                Arenas.Add(nr, new Arena(nr, v[0], v[1]));
            }

            foreach (var k in items.nations)
            {
                var v = (string[])k.Value;
                int nr = Convert.ToInt32(k.Key);
                _nations.Add(nr, new Nation(nr, v[0], v[1]));
            }



            foreach (var k in items.vehicles.list)
            {
                var v = (string[])k.Value;
                int nr = Convert.ToInt32(k.Key);
                Vehicles.Add(nr, new Vehicle(nr, v[0], v[3], v[2], v[1], v[6], v[5]));
            }
        }

        public class Vehicle
        {
            public int Id { get; private set; }
            public string Name { get; private set; }
            public int Tier { get; private set; }
            private int _typeId { get; set; }
            private int _nationId { get; set; }
            public string NameL { get; private set; }
            public string NameLShort { get; private set; }

            public VehicleType Type
            {
                get
                {
                    {
                        return WoTItems._vehicleTypes[_typeId];
                    }
                }
            }

            public Nation Nation
            {
                get
                {
                    {
                        return WoTItems._nations[_nationId];
                    }
                }
            }
            public Vehicle(int id, string name, string tier, string type, string nationId, string nameL, string nameLShort)
            {
                Id = id;
                Name = name;
                Tier = Convert.ToInt32(tier);
                _typeId = Convert.ToInt32(type);
                _nationId = Convert.ToInt32(nationId);
                NameL = nameL;
                NameLShort = nameLShort;
            }

            //for XAML Designer emtpy Constructor
            public Vehicle()
            {

            }
        }

        public class Nation
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string NameLocalized { get; set; }
            public Nation(int id, string name, string nameLocalized)
            {
                Id = id;
                Name = name;
                NameLocalized = nameLocalized;
            }
        }

        public class Arena
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string NameLocalized { get; set; }

            public Arena(int id, string name, string nameLocalized)
            {
                Id = id;
                Name = name;
                NameLocalized = nameLocalized;
            }
        }

        public class VehicleType
        {

            public int Id { get; set; }
            public string Name { get; set; }
            public string NameLong { get; set; }

            public VehicleType(int id, string name, string nameLong)
            {
                Id = id;
                Name = name;
                NameLong = nameLong;
            }
        }

        public class GameplayType
        {

            public int Id { get; set; }
            public string Name { get; set; }
            public string Descr { get; set; }
            public string DescrLong { get; set; }
            public string Job { get; set; }
            public string EnemiesJob { get; set; }

            public GameplayType(int id, string name, string descr, string descrLong, string job, string enemiesJob)
            {
                Id = id;
                Name = name;
                Descr = descr;
                DescrLong = descrLong;
                Job = job;
                EnemiesJob = enemiesJob;
            }
        }
    }
}
