using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using TouchyTickets.Attractions;
using static TouchyTickets.Attractions.AttractionFlags;
using static TouchyTickets.Attractions.AttractionType;

namespace TouchyTickets.Upgrades; 

public class Upgrade {

    public static readonly Dictionary<string, Upgrade> Upgrades = new();
    public static readonly Upgrade[] MapSize = Upgrade.RegisterTiers("MapSize", 5, 1, 1, new Point(0, 3));
    public static readonly Upgrade[] TapIncrease = Upgrade.RegisterTiers("TapIncrease", 3, 1, 0.5F, new Point(6, 3));
    public static readonly Upgrade[] ModifierIncrease = Upgrade.RegisterTiers("ModifierIncrease", 3, 1, 1.5F, new Point(9, 3));
    public static readonly Upgrade[] AutoPlaceModifiers = Upgrade.RegisterTiers("AutoPlaceModifiers", 2, 6, 1, new Point(10, 3));
    public static readonly Upgrade FerrisWheelModifier = Upgrade.Register(new NeighborModifierUpgrade("FerrisWheelModifier", 1, new Point(2, 3), AttractionType.FerrisWheel, 2));
    public static readonly Upgrade NatureModifier = Upgrade.Register(new ModifierUpgrade("NatureModifier", 1, new Point(8, 3), AttractionFlags.NonTechnology, 2));
    public static readonly Upgrade FoodCourtModifier = Upgrade.Register(new NeighborModifierUpgrade("FoodCourtModifier", 2, new Point(1, 3), AttractionType.FoodCourt, 2));
    public static readonly Upgrade RollerCoasterModifier = Upgrade.Register(new ModifierUpgrade("RollerCoasterModifier", 2, new Point(3, 3), AttractionFlags.FastCars, 2));
    public static readonly Upgrade ManualRideModifier = Upgrade.Register(new ModifierUpgrade("ManualRideModifier", 2, new Point(4, 3), AttractionFlags.Walking, 3));
    public static readonly Upgrade SpiralSlideModifier = Upgrade.Register(new NeighborModifierUpgrade("SpiralSlideModifier", 2, new Point(5, 3), AttractionType.SpiralSlide, 2));
    public static readonly Upgrade HauntedHouseModifier = Upgrade.Register(new NeighborModifierUpgrade("HauntedHouseModifier", 2, new Point(7, 3), AttractionType.HauntedHouse, 3, AttractionFlags.Relaxed));

    public readonly string Name;
    public readonly int Price;
    public readonly Point Texture;
    public readonly Upgrade[] Dependencies;

    public Upgrade(string name, int price, Point texture, params Upgrade[] dependencies) {
        this.Name = name;
        this.Price = price;
        this.Texture = texture;
        this.Dependencies = dependencies;
    }

    public void OnApplied() {
        // map size upgrades
        if (Upgrade.MapSize.Contains(this)) {
            var oldMap = GameImpl.Instance.Map;
            GameImpl.Instance.Map = oldMap.Copy(oldMap.Width + 10, oldMap.Height + 10);
        }
    }

    public bool IsActive() {
        return GameImpl.Instance.AppliedUpgrades.Contains(this);
    }

    private static Upgrade Register(Upgrade upgrade) {
        Upgrade.Upgrades.Add(upgrade.Name, upgrade);
        return upgrade;
    }

    private static Upgrade[] RegisterTiers(string name, int amount, int startPrice, float priceIncrease, Point texture) {
        var ret = new Upgrade[amount];
        for (var i = 0; i < amount; i++) {
            // every tier except first depends on last tier
            var deps = i == 0 ? Array.Empty<Upgrade>() : new[] {ret[i - 1]};
            var price = (startPrice * (1 + i * priceIncrease)).Floor();
            Upgrade.Register(ret[i] = new Upgrade(name + (i + 1), price, texture, deps));
        }
        return ret;
    }

}