using System.Collections.Generic;
using UnityEngine;

public class Global
{

    // TODO !!! debug flags
    public static bool IS_DEBUG = false;
    public static bool DEBUG_IGNORE_BUILDING_COST = false;

    public const int ITEM_WOOD = 0;
    public const int ITEM_COAL = 1;
    public const int ITEM_STONE = 2;
    public const int ITEM_COPPER_ORE = 3;
    public const int ITEM_COPPER_INGOT = 4;
    public const int ITEM_IRON_ORE = 5;
    public const int ITEM_IRON_INGOT = 6;
    public const int ITEM_GOLD_ORE = 7;
    public const int ITEM_GOLD_INGOT = 8;
    public const int ITEM_TITANIUM_ORE = 9;
    public const int ITEM_TITANIUM_INGOT = 10;
    public const int ITEM_BEET = 11;
    public const int ITEM_COUNT = 12; // !

    public static string[] ItemNames = new string[]
    {
        "Wood",
        "Coal",
        "Stone",
        "Copper Ore",
        "Copper Ingot",
        "Iron Ore",
        "Iron Ingot",
        "Gold Ore",
        "Gold Ingot",
        "Titanium Ore",
        "Titanium Ingot",
        "Beet"
    };

    public static int[] ItemValue = new int[]
    {
        1, // wood
        3, // coal
        2, // stone
        5, // copper ore
        10, // copper ingot
        25, // iron ore
        50, // iron ingot
        100, // gold ore
        200, // gold ingot
        500, // titanium ore
        1000, // titanium ingot
        75, // beet
    };

    public const int BUILDING_CHARCOAL_KILN = 0;
    public const int BUILDING_FURNACE = 1;
    public const int BUILDING_COUNT = 2; // !

    public const int RESOURCE_ORE_COPPER = 0;
    public const int RESOURCE_TREE_BIRCH = 1;
    public const int RESOURCE_TREE_SHRUB = 2;
    public const int RESOURCE_ORE_IRON = 3;
    public const int RESOURCE_ORE_GOLD = 4;
    public const int RESOURCE_ORE_TITANIUM = 5;
    public const int RESOURCE_STONE = 6;
    public const int RESOURCE_BEET = 7;
    public const int RESOURCE_COUNT = 8; // !

    public const int BIOME_GRASS = 0;
    public const int BIOME_DIRT = 1;
    public const int BIOME_COUNT = 2; // !

    // TODO !!! for final: choose top one
    public static int[] UPGRADE_COST = new int[] { 0, 10, 50, 100, 250, 500, 1000, 5000, 25000, 60000, 100000 };
    //public static int[] UPGRADE_COST = new int[] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

    public const int NODE_MAX_COUNT = 50;
    public const int NODE_INITIAL_COUNT = 30;

    public const int TILE_LENGTH = 20;

    public const float PICK_UP_BLOCK_DURATION = 0.5f;
    public const float PICK_UP_RANGE = 2f;

    public static System.Random Rng = new System.Random();

    public static Index PositionToIndex(Vector3 position)
    {
        var x = (int)System.Math.Floor((position.x + 10f) / 20f);
        var y = (int)System.Math.Floor((position.z + 10f) / 20f);
        return new Index(x, y);
    }

    public static IList<T> Shuffle<T>(IList<T> list)
    {
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = Global.Rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
        return list;
    }

    public static Vector3 GetExplosiveVelocity(float speed)
    {
        var direction = Random.insideUnitSphere;
        if (direction.y < 0f)
            direction.y *= -1;
        return speed * direction;
    }

    public static Vector3 GetRandomFlatOffset(float magnitude)
    {
        return new Vector3(Random.Range(-magnitude, magnitude), 0f, Random.Range(-magnitude, magnitude));
    }

    public static Vector3 GetRandomPositionOnTile(Tile tile, float verticalOffset)
    {
        var rangeX = TILE_LENGTH / 2 - 1;
        var rangeZ = TILE_LENGTH / 2 - 1;
        var x = tile.Element.position.x + Random.Range(-rangeX, rangeX);
        var y = tile.Element.position.y + verticalOffset;
        var z = tile.Element.position.z + Random.Range(-rangeZ, rangeZ);
        return new Vector3(x, y, z);
    }

    public static int GetUpgradeCost(int currentUpgrade)
    {
        if (currentUpgrade + 1 < 0 || currentUpgrade + 1 >= UPGRADE_COST.Length)
            return -1;
        return UPGRADE_COST[currentUpgrade + 1];
    }

}
