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

public class DrawControl : MonoBehaviour
{
    public void Clear()
    {
        drawScript_.Clear();

        arrowToggle_.isOn = false;
        textToggle_.isOn = false;
        brushToggle_.isOn = false;
        targetToggle_.isOn = false;
    }

    // Use this for initialization
    void Start()
    {
        arrowToggle_ = GameObject.Find("ArrowToggle").GetComponent<Toggle>();
        arrowToggle_.onValueChanged.AddListener(OnArrowToggleValueChanged);

        textToggle_ = GameObject.Find("TextToggle").GetComponent<Toggle>();
        textToggle_.onValueChanged.AddListener(OnTextToggleValueChanged);

        brushToggle_ = GameObject.Find("BrushToggle").GetComponent<Toggle>();
        brushToggle_.onValueChanged.AddListener(OnBrushToggleValueChanged);

        targetToggle_ = GameObject.Find("TargetToggle").GetComponent<Toggle>();
        targetToggle_.onValueChanged.AddListener(OnTargetToggleValueChanged);

        drawScript_ = FindObjectOfType<DrawScript>();
    }

    private void OnArrowToggleValueChanged(bool isOn)
    {
        ColorBlock cb = arrowToggle_.colors;
        if (isOn)
        {
            cb.normalColor = Color.gray;
            cb.highlightedColor = Color.gray;

            textToggle_.isOn = false;
            brushToggle_.isOn = false;
            targetToggle_.isOn = false;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.ARROW;
        }
        else
        {
            cb.normalColor = Color.white;
            cb.highlightedColor = Color.white;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.NO_DRAW;
        }
        arrowToggle_.colors = cb;
    }

    private void OnTextToggleValueChanged(bool isOn)
    {
        ColorBlock cb = textToggle_.colors;
        if (isOn)
        {
            cb.normalColor = Color.gray;
            cb.highlightedColor = Color.gray;

            arrowToggle_.isOn = false;
            brushToggle_.isOn = false;
            targetToggle_.isOn = false;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.TEXT;
        }
        else
        {
            cb.normalColor = Color.white;
            cb.highlightedColor = Color.white;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.NO_DRAW;
        }
        textToggle_.colors = cb;
    }

    private void OnBrushToggleValueChanged(bool isOn)
    {
        ColorBlock cb = brushToggle_.colors;
        if (isOn)
        {
            cb.normalColor = Color.gray;
            cb.highlightedColor = Color.gray;

            arrowToggle_.isOn = false;
            textToggle_.isOn = false;
            targetToggle_.isOn = false;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.PAINTBRUSH;
        }
        else
        {
            cb.normalColor = Color.white;
            cb.highlightedColor = Color.white;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.NO_DRAW;
        }
        brushToggle_.colors = cb;
    }

    private void OnTargetToggleValueChanged(bool isOn)
    {
        ColorBlock cb = targetToggle_.colors;
        if (isOn)
        {
            cb.normalColor = Color.gray;
            cb.highlightedColor = Color.gray;

            arrowToggle_.isOn = false;
            textToggle_.isOn = false;
            brushToggle_.isOn = false;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.TARGET;
        }
        else
        {
            cb.normalColor = Color.white;
            cb.highlightedColor = Color.white;

            drawScript_.DrawMode = DrawScript.DRAW_MODE.NO_DRAW;
        }
        targetToggle_.colors = cb;
    }

    Toggle arrowToggle_;
    Toggle textToggle_;
    Toggle brushToggle_;
    Toggle targetToggle_;

    DrawScript drawScript_;
}
