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

public class ToolsController : MonoBehaviour
{
    public GameObject Cursor;
    public int Rounding = 1;

    public Button Mouse;
    public Button Hammer;
    public Button Undo;
    public Button Redo;
    public Button ButtonGrid;
    public Button ButtonBeacon;
    public Button ButtonZone;
    public Button ButtonBuild;
    public GameObject GroundSettings;
    public GameObject Grid;
    public GameObject BuildTools;
    public GameObject ZonesScroll;
    public GameObject BeaconsScroll;

    Button old_btn_tools;
    Button old_btn_build;
    GameObject old_panel;

    bool cursor_active;
    bool ground_active;

    enum Tools { Wall, Room, Del, Cursor};
   // Tools tools = Tools.Cursor;


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

    // Update is called once per frame
    void Update()
    {
        if (EventSystem.current.IsPointerOverGameObject())
        {
            if (cursor_active) Cursor.SetActive(!cursor_active);
        }
        else if (cursor_active) Cursor.SetActive(cursor_active);
        if (ground_active != GroundSettings.activeSelf) GroundSettings.SetActive(ground_active);
    }

    public void CursorOff()
    {
        cursor_active=false;
        WallCreate.mode = WallCreate.Mode.Cursor;
    }

    public void WallEdit()
    {
        cursor_active = true;
        WallCreate.mode = WallCreate.Mode.Wall;
        ground_active = false;
    }


    public void RoomEdit()
    {
        cursor_active = true;
        WallCreate.mode = WallCreate.Mode.Room;
        ground_active = false;
    }

    public void BuildEdit()
    {
        SelectButton(ButtonBuild, old_btn_build);
        if (old_panel != null) old_panel.SetActive(false);
        BuildTools.SetActive(true);
        old_panel = BuildTools;
        old_btn_build = ButtonBuild;
    }

    public void GroundEdit()
    {
        if (cursor_active)
        {
            Cursor.SetActive(false);
            cursor_active = false;
        }
        ground_active = true;
    }

    public void ZoneEdit()  
    {
        cursor_active = true;
        SelectButton(ButtonZone, old_btn_build);
        WallCreate.mode = WallCreate.Mode.Zone;
        if (old_panel != null) old_panel.SetActive(false);
        ZonesScroll.SetActive(true);
        old_panel = ZonesScroll;
        old_btn_build = ButtonZone;
        ground_active = false;
    }  
    
    public void BeaconEdit()  
    {
        if (cursor_active)
        {
            Cursor.SetActive(false);
            cursor_active = false;
        }

        SelectButton(ButtonBeacon,old_btn_build);
        if (old_panel != null) old_panel.SetActive(false);
        BeaconsScroll.SetActive(true);
        old_panel = BeaconsScroll;
        old_btn_build = ButtonBeacon;
        ground_active = false;
    }


    bool grid_active = false;
    public void GridOnOff()
    {
        grid_active = !grid_active;
        Grid.SetActive(grid_active);
        if(grid_active) ButtonGrid.GetComponent<Image>().color = Color.gray;
        else ButtonGrid.GetComponent<Image>().color = Color.white;
    }

    public void Delete()
    {
        //tools = Tools.Del;
    }

    public void SelectButton(GameObject button)
    {
        GameObject old_button = null;
        switch (WallCreate.mode)
        {
            case WallCreate.Mode.Cursor:
                old_button = transform.GetChild(0).gameObject;
                break;
            case WallCreate.Mode.Room:
                old_button = transform.GetChild(1).gameObject;
                break;           
            case WallCreate.Mode.Wall:
                old_button = transform.GetChild(2).gameObject;
                break; 
            case WallCreate.Mode.Zone:
                old_button = transform.GetChild(3).gameObject;
                break;
        }

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

    public static void SelectButton(Button button, Button old_button)
    {
        var color = new Color();
        ColorUtility.TryParseHtmlString("#C8C8C8", out color);
        if (old_button != null) old_button.GetComponent<Image>().color = Color.white;
        button.GetComponent<Image>().color = color;
    }

    public void mouse()
    {
        SelectButton(Mouse, old_btn_tools);
        old_btn_tools = Mouse;
    }

    public void hammer()
    {
        SelectButton(Hammer, old_btn_tools);
        old_btn_tools = Hammer;
    }

    public void undo()
    {
        SelectButton(Undo, old_btn_tools);
        old_btn_tools = Undo;
    }

    public void redo()
    {
        SelectButton(Redo, old_btn_tools);
        old_btn_tools = Redo;
    }

    public void OpenFileBrowser()
    {
       StartCoroutine(ShowLoadDialogCoroutine($"Выбор карты"));
    }

    IEnumerator ShowLoadDialogCoroutine(string name_view)
    {
        FileBrowser.SetFilters(true, new FileBrowser.Filter("Images", ".jpg", ".png"));
        FileBrowser.SetDefaultFilter(".jpg");
        //FileBrowser.AddQuickLink("Users", "C:\\Users", null);

        // Show a load file dialog and wait for a response from user
        // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load"
        yield return FileBrowser.WaitForLoadDialog(false, null, name_view, "Load");

        // Dialog is closed
        // Print whether a file is chosen (FileBrowser.Success)
        // and the path to the selected file (FileBrowser.Result) (null, if FileBrowser.Success is false)
        Debug.Log(FileBrowser.Success + " " + FileBrowser.Result);

        if (FileBrowser.Success)
        {
            var go = GameObject.Find("InputField_Image");
            if (go != null)
            {
                var ifield = go.GetComponent<InputField>();
                if (ifield != null)
                {
                    ifield.text = FileBrowser.Result;
                }
            }
            // If a file was chosen, read its bytes via FileBrowserHelpers
            // Contrary to File.ReadAllBytes, this function works on Android 10+, as well
            byte[] bytes = FileBrowserHelpers.ReadBytesFromFile(FileBrowser.Result);
            var loc = CompanyController.instance.GetActiveLocation();
            if (loc != null)
            {
                loc.SaveTexture(bytes);
            }
        }
    }
}
