using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class DroneCreationController : MonoBehaviour {

    public Toggle[] camToggles;
    public Toggle[] cpuToggles;
    public Toggle[] toolToggles;

    public Dictionary<Toggle, CameraClass> cameraOptions;
    public Dictionary<Toggle, CPUClass> cpuOptions;
    public Dictionary<Toggle, Tool> toolOptions;

    public ToggleGroup cameraGroup;
    public ToggleGroup cpuGroup;
    public ToggleGroup toolGroup;
    public Slider capacitySlider;
    public Slider fillSlider;

    public Text capacityText;
    public Text fillText;
    public Text createButtonText;

    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public void Awake()
    {
        cameraOptions = new Dictionary<Toggle, CameraClass>();
        cpuOptions = new Dictionary<Toggle, CPUClass>();
        toolOptions = new Dictionary<Toggle, Tool>();

        int i = 0;
        foreach (CameraClass c in Enum.GetValues(typeof(CameraClass)))
        {
            cameraOptions[camToggles[i++]] = c;
        }
        i = 0;
        foreach (CPUClass c in Enum.GetValues(typeof(CPUClass)))
        {
            cpuOptions[cpuToggles[i++]] = c;
        }
        i = 0;
        foreach (Tool t in Enum.GetValues(typeof(Tool)))
        {
            toolOptions[toolToggles[i++]] = t;
        }
    }

    public void Update()
    {
        var camClass = cameraOptions[cameraGroup.ActiveToggles().First()];
        var cpu = cpuOptions[cpuGroup.ActiveToggles().First()];
        var tool = toolOptions[toolGroup.ActiveToggles().First()];

        int capacity = Mathf.RoundToInt(capacitySlider.value);
        int fill = Mathf.RoundToInt(fillSlider.value);
        int percentage = Mathf.RoundToInt(fill * 100f / capacity);
        
        capacityText.text = "Capacity: " + capacity + " kJ";
        fillText.text = "Fill: " + fill + " kJ  (" + percentage + "%)";

        fillSlider.maxValue = capacity;

        var cost = DroneController.GetCost(camClass, cpu, tool, capacity, fill);
        createButtonText.text = "Create New Drone (" + cost + " kJ)";
    }

    public void Create()
    {
        var camClass = cameraOptions[cameraGroup.ActiveToggles().First()];
        var cpu = cpuOptions[cpuGroup.ActiveToggles().First()];
        var tool = toolOptions[toolGroup.ActiveToggles().First()];

        int capacity = Mathf.RoundToInt(capacitySlider.value);
        int fill = Mathf.RoundToInt(fillSlider.value);

        string dcName;

        do
        {
            dcName = "VAC" + new string(Enumerable.Repeat(chars, 2).Select(s => s[Random.Range(0, s.Length)]).ToArray()) + Random.Range(10, 100);
        }
        while (Game.instance.drones.Any(d => d.name == dcName));

        var dc = DroneController.Create(Game.instance.dronePrefab, dcName, camClass, cpu, tool, capacity, fill);
        if (dc == null)
            return;
        Game.instance.RegisterDrone(dc);
        DroneController.active = dc;
    }
}
