using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/// <summary>
/// Редактор
/// </summary>
public class EditorController : MonoBehaviour
{
    public InputField Name;
    public InputField Image;
    public InputField SizeX;
    public InputField SizeY;
    public GameObject plane;
    public GameObject BeaconsContent;
    public GameObject PanelBeaconEdit;
    public Toggle ToggleGrid;
    public Toggle ToggleScalePanel;
    public Toggle projection;
    public GameObject Grid;

    public List<Beacon> Beacons = new List<Beacon>();

    PlayerController player;
    ModeController mode;
    ToolsController tools;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Canvas").transform.GetChild(2).GetComponent<PlayerController>();
        mode = GameObject.Find("ButtonMode").GetComponent<ModeController>();
        tools = GameObject.Find("EditorTools").GetComponent<ToolsController>();
    }

    // Update is called once per frame
    void Update()
    {
        SizeX.text = $"{plane.transform.localScale.x}";
        SizeY.text = $"{plane.transform.localScale.z}";
        if (ToggleGrid.isOn != Grid.activeSelf) Grid.SetActive(ToggleGrid.isOn);
        if (ToggleScalePanel.isOn != plane.GetComponent<TouchScript>().activeScript) plane.GetComponent<TouchScript>().activeScript = ToggleScalePanel.isOn;

        if (projection.isOn)
        {
            Camera.main.orthographic = true;
        }
        else
        {
            Camera.main.orthographic = false;
        }
    }

    public void UpdatePanel()
    {
        if (!Image.text.Equals(""))
            StartCoroutine(SendingFormController.LoadImage(Image.text, plane));

        var x = 1f;
        if (!SizeX.text.Equals(""))
            x = float.Parse(SizeX.text, CultureInfo.InvariantCulture);
        var y = 1f;
        if (!SizeY.text.Equals(""))
            y = float.Parse(SizeY.text, CultureInfo.InvariantCulture);
        plane.transform.localScale = new Vector3(x, 1, y); 
        var scale = plane.transform.localScale;
        plane.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
    }

    public void Save()
    {
        //SceneManager.LoadScene("Location");
        tools.CursorOff();
        mode.Switch();
        var l = new Location { id = PlayerController.locations.Count, name = Name.text };
        PlayerController.locations.Add(l);
        player.DropdownLocation.options.Add(new Dropdown.OptionData(l.name));
        var locations = GameObject.Find("Locations").gameObject;
        var location = new GameObject();
        location.name = l.name;
        location.transform.SetParent(locations.transform);
        foreach (var w in WallCreate.Walls)
            w.transform.SetParent(location.transform);
        player.maps.Add(location);
    }

    public void Back()
    {
        //SceneManager.LoadScene("Location");
        mode.Switch();
    }

    public void AddBeacon()
    {
        PanelBeaconEdit.SetActive(true);
        PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = $"Маяк_{Beacons.Count}";
        var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
        beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;
        var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
        panel_button.transform.SetParent(BeaconsContent.transform);
        var button = panel_button.transform.GetChild(0).GetComponent<Button>();      
        button.name = $"BeaconButton_{Beacons.Count}";
        
        button.transform.GetChild(0).GetComponent<Text>().text = $"Маяк_{Beacons.Count}";

        var b = new Beacon { };

        button.onClick.AddListener(() =>
        {
            PanelBeaconEdit.SetActive(true);
        });
        //var text = PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = "";
        //var uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text = "";
        var button_ok = PanelBeaconEdit.transform.GetChild(5).transform.GetChild(0).GetComponent<Button>();
        button_ok.onClick.AddListener(() =>
        {
            b.uuid = PanelBeaconEdit.transform.GetChild(1).GetComponent<InputField>().text;
            b.minor = uint.Parse(PanelBeaconEdit.transform.GetChild(2).GetComponent<InputField>().text);
            b.major = uint.Parse(PanelBeaconEdit.transform.GetChild(3).GetComponent<InputField>().text);

            PanelBeaconEdit.SetActive(false);
        });
        var button_close = PanelBeaconEdit.transform.GetChild(5).transform.GetChild(1).GetComponent<Button>();
        button_close.onClick.AddListener(() =>
        {
            PanelBeaconEdit.SetActive(false);
        });

         var button_del = panel_button.transform.GetChild(1).GetComponent<Button>();
        button_del.onClick.AddListener(() =>
        {
            Destroy(beacon);
            Destroy(panel_button);
            Beacons.Remove(b);
            if (PanelBeaconEdit.activeSelf) PanelBeaconEdit.SetActive(false);
        });

        b.id = (uint)Beacons.Count;
        b.beacon = beacon;
        b.button = button;

        Beacons.Add(b);
    }


}
