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

/// <summary>
/// опт
/// </summary>
public class BeaconsEditorController : MonoBehaviour
{
    public Dictionary<uint, Beacon> Beacons = new Dictionary<uint, Beacon>();

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

    // Update is called once per frame
    void Update()
    {
        
    }

    public Beacon CreateBeacon(uint loaction_id)
    {
        var beacon = new Beacon
        {
            id = Convert.ToUInt32(4000000000 + Beacons.Count),
            location_id = loaction_id,
            enabled = true
        };
        AddBeacon(beacon);
        return beacon;
    }

    public void EditBeacon(Beacon beacon, string name)
    {

    }

    /// <summary>
    /// опт
    /// </summary>
    /// <param name="b"></param>
    public void AddBeacon(Beacon b)
    {
        var beacon = Instantiate(Resources.Load("GameObjects/Beacon", typeof(GameObject))) as GameObject;
        if (ModeController.editor) beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;

        beacon.name = $"Beacon_{b.id}";

        beacon.transform.position = new Vector3(b.x, 0.5f, b.y);

        var beaconController = beacon.GetComponent<BeaconController>();
        beaconController.InformationPanel(b);
        beaconController.id = $"{b.id}";
        beaconController.BeaconEnabled(b, b.enabled);

        beacon.transform.SetParent(transform);

        var name_beacon = $"Маяк {b?.id.ToString() ?? Beacons.Count.ToString()}";
        //PanelBeaconEdit.transform.GetChild(0).GetComponent<Text>().text = name_beacon;
        var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject;
        //
        panel_button.name = $"Beacon_{b?.id.ToString() ?? Beacons.Count.ToString()}";
        var button = panel_button.transform.GetChild(0).GetComponent<Button>();
        button.name = $"BeaconButton_{Beacons.Count}";

        button.transform.GetChild(0).GetComponent<Text>().text = name_beacon;

        b.beacon = beacon;
        b.panel_button = panel_button;

        Beacons.Add(b.id, b);
    }

    public void SetParent(Transform parent)
    {
        foreach (var b in Beacons)
            b.Value.panel_button.transform.SetParent(parent);
    }

    public void DeleteBeacon(Beacon b)
    {
        Destroy(b.beacon);
        Destroy(b.panel_button);
        Beacons.Remove(b.id);
    }

    public void DeleteAllBeacons()
    {
        foreach (var b in Beacons)
            DeleteBeacon(b.Value);
    }

    
}
