using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using static TimelineController;

public enum DataType
{
    None,
    Object,
    Character,
    Camera,
    Scene
}

public class TimelineFrameContainer : MonoBehaviour
{
    public int FrameIndex;
    public TMPro.TextMeshProUGUI FrameLabel;

    public ObjectContainer TrackedObject;
    public FrameContent FrameContents;
    
    public Button DeleteButton;
    public Button CopyButton;
    public GameObject Controls;

    private bool
        _copyEnabled,
        _deleteEnabled;

    public Color Color
    {
        get => GetComponent<Image>().color;
        set => GetComponent<Image>().color = value;
    }

    public TimelineFrameContainer SetNum(int frameNum)
    {
        FrameIndex = frameNum;
        FrameLabel.text = frameNum.ToString();
        return this;
    }

    public void UpdateContent(ObjectContainer trackedObject, FrameContent content)
    {
        TrackedObject = trackedObject;
        FrameContents = content;

        if(trackedObject == null || content == null)
        {
            _copyEnabled = false;
            _deleteEnabled = false;
            Color = (FrameIndex == TimelineController.Instance.CurrentFrame) ? Color.white : Color.gray / 2;
            DeleteButton.gameObject.SetActive(false);
            CopyButton.gameObject.SetActive(false);
            Controls.SetActive(false);
        }
        else
        {
            _copyEnabled = true;
            _deleteEnabled = true;
            Color = (FrameIndex == TimelineController.Instance.CurrentFrame) ? Color.white : Color.gray;
            DeleteButton.gameObject.SetActive(true);
            CopyButton.gameObject.SetActive(true);
            Controls.SetActive(true);
        }
    }

    public void SwapFrame(int dir)
    {
        TimelineController.SwapFrames(FrameIndex, FrameIndex + dir);
    }

    public void PasteFrame()
    {
        int frameNum = FrameIndex;
        var data = TimelineController.Instance.CopyBuffer;

        if (data == null) return;
        if (GetCurrentObject() != data.TrackedObject)
        {
            Error.Log(Color.red, "You can only paste to the same object.");
            Debug.Log("Buffer: " + data.TrackedObject);
            Debug.Log("Current: " + GetCurrentObject());
            return;
        }

        var trackedObject = data.TrackedObject;
        if (trackedObject == null)
        {
            Error.Log(Color.red, "Object has been deleted?");
            return;
        }

        if (!trackedObject.GetCurrentFrame(frameNum, out var currentFrame))
        {
            currentFrame.SetObjectData(data.Content.ObjectData);
            SetCurrentFrame(frameNum);
            return;
        }

        UIPopupMessage.Create($"Replace existing data for {trackedObject.name}?",
            () => {
                currentFrame.SetObjectData(data.Content.ObjectData);
                SetCurrentFrame(frameNum);
            },
            () => { }
         );
    }

    public void CopyFrame()
    {
        if (!_copyEnabled) return;
        TimelineController.Instance.CopyBuffer = new CopyBufferData()
        {
            Content = FrameContents,
            TrackedObject = TrackedObject
        };
    }

    public void DeleteFrame()
    {
        if (!_deleteEnabled) return;
        if (TrackedObject.Frames.Count <= 1) return;
        TrackedObject.Frames.Remove(FrameContents);
        TimelineController.SetCurrentFrame(TimelineController.Instance.CurrentFrame);
    }
}
