using UnityEngine;
using UnityEngine.UI;

public class CameraSettings : MonoBehaviour, IKeyframeSetter
{
    protected bool _setKeyframe = true;

    [Header("Scene")]
    public SliderPanel
        SliderFov;
    public SliderPanel
        SliderCamDist,
        SliderCamHeight,
        SliderTargetHeight,
        SliderCamAngle;

    [Header("Unsaved")]
    public SliderPanel
        SliderMovSpeed;
    public SliderPanel
        SliderRotSpeed,
        SliderZoomSpeed;

    public Toggle ToggleCameraLock;
    public Toggle ToggleWorkCamera;

    public ExtendedDropdown BackgroundDropdown;
    public TMPro.TMP_Dropdown DropdownCameraMode;

    public GameObject ColorPickerPanel;

    public int CameraMode   => DropdownCameraMode.value;

    public bool CameraLock  => ToggleCameraLock.isOn;

    public float Fov        => SliderFov.value;
    public float CamDist    => SliderCamDist.value;
    public float CamHeight  => SliderCamHeight.value;
    public float TargetHeight => SliderTargetHeight.value;
    public float CamAngle   => SliderCamAngle.value;
    public float MovSpeed   => SliderMovSpeed.value;
    public float RotSpeed   => SliderRotSpeed.value;
    public float ZoomSpeed  => SliderZoomSpeed.value;

    public void SwapCamera(bool workCameraMode)
    {
        var mainCamera = ModelViewerMain.GetInstance().MainCameraOrbit;
        var workCamera = ModelViewerMain.GetInstance().WorkCameraOrbit;

        var camera = mainCamera.Camera;

        if (workCameraMode)
        {
            mainCamera.LastPosition = mainCamera.SerializeFrame() as KeyframeDataCamera;
            mainCamera.transform.SetPositionAndRotation(camera.Cam.transform.position, camera.Cam.transform.rotation);
            mainCamera.LineRenderer.enabled = true;
            workCamera.LineRenderer.enabled = false;

            if(workCamera.LastPosition.Root.Position == Vector3.zero)
            {
                workCamera.PastePose(mainCamera.LastPosition, new PoseLoadOptions(true));
            }
            else
            {
                workCamera.PastePose(workCamera.LastPosition, new PoseLoadOptions(true));
            }
        }
        else
        {
            workCamera.LastPosition = workCamera.SerializeFrame() as KeyframeDataCamera;
            workCamera.transform.SetPositionAndRotation(camera.Cam.transform.position, camera.Cam.transform.rotation);
            //WorkCameraOrbit.LineRenderer.enabled = true;
            mainCamera.LineRenderer.enabled = false;

            mainCamera.PastePose(mainCamera.LastPosition, new PoseLoadOptions(true));
        }
    }

    public void SetKeyframe(int frameNum = -1)
    {
        if (ToggleWorkCamera.isOn || !_setKeyframe) return;

        ModelViewerMain.GetInstance().MainCameraOrbit.SetKeyframe();
    }

    public void EnableKeyframeSet()
    {
        _setKeyframe = true;
    }

    public void DisableKeyframeSet()
    {
        _setKeyframe = false;
    }

    public void OnBackgroundDropdownSelected(int index)
    {
        switch (index)
        {
            case 0:
                Camera.main.clearFlags = CameraClearFlags.Skybox;
                //ColorPickerPanel.SetActive(false);
                break;
            case 1:
                Camera.main.clearFlags = CameraClearFlags.SolidColor;
                //ColorPickerPanel.SetActive(true);
                break;
            case 2:
                Camera.main.clearFlags = CameraClearFlags.Nothing;
                //ColorPickerPanel.SetActive(false);
                break;
        }
    }

    public void SetBackgroundColor(Color color)
    {
        Camera.main.backgroundColor = color;
    }
}
