﻿using AsmodeeDigital.CrossPromo.Plugin.UI;
using AsmodeeDigital.CrossPromo.Plugin.View;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace AsmodeeDigital.CrossPromo.Plugin.Utils
{
    [RequireComponent(typeof(CanvasScaler))]
    public class DeviceScaler : MonoBehaviour
    {
        public static DeviceScaler Instance;

        public CanvasScaler CanvasScaler;
        public MoreGamesPopup MoreGamesPopup;

        private float _screenSize = 0f;
        private bool _isAllreadyResized = false;
        private ScreenOrientation _previousOrientation = ScreenOrientation.Unknown;
        private Vector2 _initialSize;

        public event Action DeviceOrientationChangedEvent;

        private void OnEnable()
        {
            Instance = this;
        }

        private void Start()
        {
            _initialSize = MoreGamesPopup.uiMoreGamesPopup.RectTransform.sizeDelta;

            if (!_isAllreadyResized)
            {
                _isAllreadyResized = true;

                if (Screen.dpi > 0)
                {
                    float w = Screen.width / Screen.dpi;
                    float h = Screen.height / Screen.dpi;

                    _screenSize = Mathf.Sqrt(w * w + h * h);

                    Debug.Log("Screen size : " + _screenSize);
                }
                else
                {
                    Debug.Log("No dpi");
                }
            }

            UpdateRotation(Screen.orientation);
            StartCoroutine(CheckScreenRotation());
        }

        protected IEnumerator CheckScreenRotation()
        {
            while (true)
            {
                ScreenOrientation screenOrientation = Screen.orientation;

                if (screenOrientation != _previousOrientation)
                {
                    _previousOrientation = screenOrientation;

                    if (screenOrientation == ScreenOrientation.Portrait || screenOrientation == ScreenOrientation.PortraitUpsideDown ||
                    screenOrientation == ScreenOrientation.LandscapeLeft || screenOrientation == ScreenOrientation.LandscapeRight)
                    {
                        UpdateRotation(screenOrientation);
                    }
                }

                yield return new WaitForSeconds(0.1f);
            }
        }

        private void UpdateRotation(ScreenOrientation screenOrientation)
        {
            bool refresh = false;
            bool wide = false;
            bool forceRefresh = false;

#if UNITY_EDITOR
            if (Screen.width > Screen.height)
                wide = true;

            forceRefresh = true;
#endif
            if ((screenOrientation == ScreenOrientation.Portrait || screenOrientation == ScreenOrientation.PortraitUpsideDown) || (forceRefresh && !wide))
            {
                Debug.Log("portrait");

                refresh = true;
                CrossPromoView.Instance.NbColumns = 3;
                CanvasScaler.matchWidthOrHeight = 0;
                CanvasScaler.referenceResolution = new Vector2(_initialSize.x + 40f, 600f);
            }
            else if ((screenOrientation == ScreenOrientation.LandscapeLeft || screenOrientation == ScreenOrientation.LandscapeRight) || (forceRefresh && wide))
            {
                Debug.Log("landscape");

                refresh = true;

                CrossPromoView.Instance.NbColumns = IsTablet() ? 4 : 3;

                CanvasScaler.matchWidthOrHeight = 1;
                CanvasScaler.referenceResolution = new Vector2(800f, 600f);
            }

            if (refresh & DeviceOrientationChangedEvent != null)
                DeviceOrientationChangedEvent();
        }

        private bool IsTablet()
        {
            return SystemInfo.deviceType == DeviceType.Handheld && (_screenSize > 6f);
            //return SystemInfo.deviceType == DeviceType.Handheld && (Input.deviceOrientation == DeviceOrientation.LandscapeLeft || Input.deviceOrientation == DeviceOrientation.LandscapeLeft) && (_screenSize > 6f);
        }
    }
}
