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

public class Editor2DController : MonoBehaviour
{
    public InputField Image;
    public InputField SizeX;
    public InputField SizeY;
    public GameObject plane;
    public GameObject BeaconsContent;
    public List<Beacon> Beacons = new List<Beacon>();

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        SizeX.text = $"{plane.transform.localScale.x}";
        SizeY.text = $"{plane.transform.localScale.z}";
    }

    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");
    }

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

  public void AddBeacon()
    {
        var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
        var button = Instantiate(Resources.Load("GameObjects/ButtonUI", typeof(Button))) as Button;
        button.name = $"BeaconButton_{Beacons.Count}";
        button.transform.SetParent(BeaconsContent.transform);
        Beacons.Add(new Beacon { id = (uint)Beacons.Count, beacon = beacon, button = button });
    }
}
