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

public class AdjacentController : MonoBehaviour
{
    public Orientation orientation;
    public Color DefaultColor = Color.white;
    public Color HighlightingColor = Color.yellow;
    private List<Text> Infos;
    private DateTime t1, t2;
    // Start is called before the first frame update
    void Start()
    {
        Observable.EveryLateUpdate()
           .Where(_=>Infos!=null)
           .Subscribe(_ =>
           {
               if (SelectionManager.GetSelectedObjects().Find(go=>go==this.gameObject))
               {
                   SetAdjecentHighlighting(Infos, true);
               }
               else
               {
                   SetAdjecentHighlighting(Infos, false);
               }
           }).AddTo(gameObject);
    }

    void OnMouseDown()
    {
        if (!EventSystem.current.IsPointerOverGameObject())
        {
            t2 = DateTime.Now;
            if (t2 - t1 < new TimeSpan(0, 0, 0, 0, 500))
            {
                UIManager.Instance.Show<AdjacentImagePanel>(GetData);
            }
            t1 = t2;
        }

    }

    private void GetData()
    {
        var view = UIManager.Instance.GetView<AdjacentImagePanel>();
        view.orientation = orientation;
        switch (orientation)
        {
            case Orientation.East:
                view.TitleText.text = "东侧";
                break;
            case Orientation.South:
                view.TitleText.text = "南侧";
                break;
            case Orientation.West:
                view.TitleText.text = "西侧";
                break;
            case Orientation.North:
                view.TitleText.text = "北侧";
                break;
        }
        
        view.OnRefresh();
        //view.LoadData();
    }

    public void GetInfos()
    {
        switch (orientation)
        {
            case Orientation.East:
                Infos = UIManager.Instance.GetView<AdjacentInfoPanel>().East;
                break;
            case Orientation.South:
                Infos = UIManager.Instance.GetView<AdjacentInfoPanel>().South;
                break;
            case Orientation.West:
                Infos = UIManager.Instance.GetView<AdjacentInfoPanel>().West;
                break;
            case Orientation.North:
                Infos = UIManager.Instance.GetView<AdjacentInfoPanel>().North;
                break;
        }
    }
    private void SetAdjecentHighlighting(List<Text> Infos, bool isOn)
    {
        foreach (var text in Infos)
        {
            if (isOn)
            {
                text.color = HighlightingColor;
            }
            else
            {
                text.color = DefaultColor;
            }
        }
    }
}