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

public class SetGameObjects : MonoBehaviour
{
    // ReSharper disable InconsistentNaming
    private readonly Color Green = new Color(0, 0.93f, 0);
    private readonly Color Red = new Color(1f, 0, 0);
    private readonly Color Blue = new Color(0, 0.3843f, 0.8745f);
    private readonly Color Yellow = new Color(1f, 1f, 0);
    // ReSharper restore InconsistentNaming


    public GameObject Background;
    public GameObject Border;
    public GameObject Title;
    public GameObject Question;
    public GameObject LeftQuestion;
    public GameObject RightQuestion;
    public GameObject Followup;
    public GameObject Timer;
    public GameObject Arrows;

    private GameData _gameData;

    private const string Angles = "anglesTexture";
    private const string Circle = "circleTexture";
    private const string Liquidish = "liquidishTexture";
    private const string Polygon = "polygonTexture";
    private const string Rectangle = "rectTexture";

    private const string BasicMath = "basicMath";
    private const string Break = "break";
    private const string FeatsOfStrenth = "featsOfStrength";
    private const string Smart = "smart";
    private const string WhichIsBetter = "whichIsBetter";

    private static Dictionary<string, Sprite> _borders;
    private static Dictionary<string, Sprite> _titles;

    // ReSharper disable once UnusedMember.Local
    void Awake()
    {
        _gameData = GetComponent<GameData>();

        if (_borders == null)
        {
            _borders = new Dictionary<string, Sprite>();
            LoadBorders();
        }

        if (_titles == null)
        {
            _titles = new Dictionary<string, Sprite>();
            LoadTitles();
        }
    }

    void LoadBorders()
    {
        var path = "Borders/";
        _borders[Angles] = Resources.Load<Sprite>(path + Angles);
        _borders[Circle] = Resources.Load<Sprite>(path + Circle);
        _borders[Liquidish] = Resources.Load<Sprite>(path + Liquidish);
        _borders[Polygon] = Resources.Load<Sprite>(path + Polygon);
        _borders[Rectangle] = Resources.Load<Sprite>(path + Rectangle);
    }

    void LoadTitles()
    {
        var path = "Titles/";
        _titles[BasicMath] = Resources.Load<Sprite>(path + BasicMath);
        _titles[Break] = Resources.Load<Sprite>(path + Break);
        _titles[FeatsOfStrenth] = Resources.Load<Sprite>(path + FeatsOfStrenth);
        _titles[Smart] = Resources.Load<Sprite>(path + Smart);
        _titles[WhichIsBetter] = Resources.Load<Sprite>(path + WhichIsBetter);
    }

    // ReSharper disable once UnusedMember.Local
    void Start()
    {
        var currentActivity = _gameData.CurrentActivity;

        SetBorder(currentActivity);
        SetBackgroundColor(currentActivity);
        SetTitleImage(currentActivity);
        SetQuestionText(currentActivity);
        SetFollowupText(currentActivity);
    }

    Sprite GetBorder(ActivityType type)
    {
        switch (type)
        {
            case ActivityType.AvB:
                return _borders[Angles];
            case ActivityType.BasicMath:
                return _borders[Polygon];
            case ActivityType.Break:
                return _borders[Liquidish];
            case ActivityType.FeatsOfStrength:
                return _borders[Rectangle];
            case ActivityType.Trivia:
                return _borders[Circle];
            default:
                return null;
        }
    }

    Sprite GetTitle(ActivityType type)
    {
        switch (type)
        {
            case ActivityType.AvB:
                return _titles[WhichIsBetter];
            case ActivityType.BasicMath:
                return _titles[BasicMath];
            case ActivityType.Break:
                return _titles[Break];
            case ActivityType.FeatsOfStrength:
                return _titles[FeatsOfStrenth];
            case ActivityType.Trivia:
                return _titles[Smart];
            default:
                return null;
        }
    }

    Color GetBackgroundColor(ActivityType type)
    {
        switch (type)
        {
            case ActivityType.AvB:
                return Green;
            case ActivityType.BasicMath:
                return Red;
            case ActivityType.Break:
                return Blue;
            case ActivityType.FeatsOfStrength:
                return Yellow;
            case ActivityType.Trivia:
                return Red;
            default:
                return new Color(1, 1, 1);
        }
    }

    private void SetBorder(Activity activity)
    {
        Border.GetComponent<Image>().sprite = GetBorder(activity.activityType);
    }

    private void SetBackgroundColor(Activity activity)
    {
        Background.GetComponent<Image>().color = GetBackgroundColor(activity.activityType);
    }

    private void SetTitleImage(Activity activity)
    {
        var titleSprite = GetTitle(activity.activityType);
        Title.GetComponent<Image>().sprite = titleSprite;

        RectTransform rectangle = Title.GetComponent<RectTransform>();
        rectangle.sizeDelta = new Vector2(titleSprite.rect.width, titleSprite.rect.height);
    }

    void SetQuestionText(Activity activity)
    {
        Question.SetActive(false);
        LeftQuestion.SetActive(false);
        RightQuestion.SetActive(false);

        if (activity.activityType == ActivityType.AvB)
        {
            LeftQuestion.SetActive(true);
            RightQuestion.SetActive(true);
            Arrows.SetActive(true);

            LeftQuestion.GetComponent<Text>().text = activity.text;
            RightQuestion.GetComponent<Text>().text = activity.text2;
        }
        else
        {
            Question.SetActive(true);

            Question.GetComponent<Text>().text = activity.text;
        }
    }
    private void SetFollowupText(Activity activity)
    {
        if (_gameData.HasFollowupText())
        {
            Followup.GetComponent<Text>().text = activity.followup;
        }
    }

    public void DeactiveTimer()
    {
        Timer.SetActive(false);
    }
}
