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

public class ModeController : MonoBehaviour
{
    //public enum Mode { Player, Editor};
    //public Mode mode = Mode.Player;
    public GameObject Company;
    public GameObject Player;
    public GameObject Editor;
    public GameObject EditorTools;
    public GameObject LineX;
    public GameObject LineY;
    public GameObject Markers;
    public GameObject WallCursor;
    public GameObject DropdownLocations;
    public GameObject Building;
    public GameObject BuildingMode;
    public GameObject GroundSettings;

    Transform maps;

    public static bool editor = false;
    Button button;

    Dropdown.OptionData item;

    // Start is called before the first frame update
    void Start()
    {
        button = gameObject.GetComponent<Button>();
        button.onClick.AddListener(() => {
            Switch();
        });

        if (editor) EditorMode();

        maps = DropdownLocations.transform;
    }

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

    public void Switch()
    {
        editor = !editor;
        transform.GetChild(0).gameObject.SetActive(!editor);
        transform.GetChild(1).gameObject.SetActive(editor);

        if (editor)
        {
            Player.GetComponent<PlayerController>().StopProgress();
            EditorMode();
        }
        else PlayerMode();
    }

    void PlayerMode()
    {
        var company = Company.GetComponent<CompanyController>();
        var l = company.locations[company.locations_index[company.active_location]];
        foreach (var b in l.beacons)
            b.beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Player;

        var edit = Editor.GetComponent<EditorController>();
        Destroy(edit.NewLocation);
        edit.EditLocation = null;

        Player.SetActive(true);
        //Locations.SetActive(true);
        Markers.SetActive(true);
        //Beacons.SetActive(true);
        Editor.SetActive(false);
        EditorTools.SetActive(false);
        Building.SetActive(false);
        if (BuildingMode.activeSelf == true) BuildingMode.SetActive(false);
        if (GroundSettings.activeSelf == true) GroundSettings.SetActive(false);

        Destroy(GameObject.Find("SizeX"));
        Destroy(GameObject.Find("SizeY"));

        if (item == null)
        {
            item = maps.GetComponent<Dropdown>().options.Last();
            maps.GetComponent<Dropdown>().options.Remove(item);
            maps.GetComponent<Dropdown>().RefreshShownValue();
        }        

        //for (int i = 0; i < Beacons.transform.childCount; i++)
        //{
        //    Beacons.transform.GetChild(i).GetComponent<BeaconController>().mode = BeaconController.Mode.Player;
        //    Beacons.transform.GetChild(i).GetComponent<LabelObjectScript>().enabled = true;
        //}

        WallCursor.SetActive(false);

        GameObject old_button = null;
        switch (WallCreate.mode)
        {
            case WallCreate.Mode.Cursor:
                old_button =EditorTools.transform.GetChild(0).gameObject;
                break;
            case WallCreate.Mode.Room:
                old_button = EditorTools.transform.GetChild(1).gameObject;
                break;
            case WallCreate.Mode.Wall:
                old_button = EditorTools.transform.GetChild(2).gameObject;
                break;
        }

        if (old_button != null) old_button.GetComponent<Image>().color = Color.white;

        //PlayerController.markers = new List<Marker>();
        //client.SendGetUsers((uint)PlayerController.active_company + 1);

        //PlayerController.Beacons = new List<Beacon>(); 
        //client.BeaconsRequest((uint)PlayerController.locations[DropdownLocation.value].id);
    }

    void EditorMode()
    {
        Player.SetActive(false);
        //Locations.SetActive(false);
        Markers.SetActive(false);
        //Beacons.SetActive(false);
        Editor.SetActive(true);
        EditorTools.SetActive(true); 
        Building.SetActive(true);
        BuildingMode.SetActive(true);

        var company = Company.GetComponent<CompanyController>();
        var l = company.locations[company.locations_index[company.active_location]];
        foreach (var b in l.beacons)
            b.beacon.GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;

        //item = new Dropdown.OptionData("Новая локация");
        //maps.GetComponent<Dropdown>().options.Add(item);

        //for (int i = 0; i < Beacons.transform.childCount; i++)
        //{ 
        //    Beacons.transform.GetChild(i).GetComponent<BeaconController>().info = false;
        //    Beacons.transform.GetChild(i).GetComponent<BeaconController>().mode = BeaconController.Mode.Editor;     
        //    Beacons.transform.GetChild(i).GetComponent<LabelObjectScript>().enabled = false;                
        //}

        var color = new Color();
        ColorUtility.TryParseHtmlString("#C8C8C8", out color);
        EditorTools.transform.GetChild(0).gameObject.GetComponent<Image>().color = color;

        //foreach (var m in PlayerController.markers)
        //{
        //    Destroy(m.marker.gameObject);
        //    Destroy(m.marker_line.gameObject);
        //    Destroy(m.toggle.gameObject);
        //}
        //PlayerController.markers.Clear();

        //foreach (var b in PlayerController.Beacons)
        //{
        //    Destroy(b.beacon);
        //    Destroy(b.button);
        //}
        //PlayerController.Beacons.Clear();

    }
}
