﻿using AsmodeeDigital.Common.Plugin.Utils;
using AsmodeeDigital.CrossPromo.Plugin.Domain;
using AsmodeeDigital.CrossPromo.Plugin.Domain.Data;
using AsmodeeDigital.CrossPromo.Plugin.Logic;
using AsmodeeDigital.CrossPromo.Plugin.Manager;
using AsmodeeDigital.CrossPromo.Plugin.UI;
using Assets.Plugins.AsmodeeDigital.CrossPromo.Code.UI;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace AsmodeeDigital.CrossPromo.Plugin.View
{
    [ExecuteInEditMode]
    public class CrossPromoView : MonoBehaviour
    {
        public string ScreenNameForAnalytics = "home_screen";

        [Serializable]
        public class UI
        {
            public Banner BannerImage;
            public MoreGamesPopup MoreGamesPopup;
            public GameDetailsPopup GameDetailsPopup;
            public InterstitialPopup InterstitialPopup;
            public MessagePopup MessagePopup;
            public Canvas Canvas;
            public CanvasScaler CanvasScaler;
            public Image Background;
            public Image BackgroundContainer;
        }

        public static CrossPromoView Instance;
        public CrossPromoLogic CrossPromoLogic;

        public UI ui;
        public CrossPromoSkin Skin;

        [HideInInspector]
        public int NbColumns = 3;

        [Header("Background")]
        public float ModalBackgroundAlpha = 0.6f;
        public float ModalBackgroundAnimationDuration = 0.2f;

        private const string ContextCrossPromoInterstitial = "cross_promo_interstitial";
        private const string ContextCrossPromoMoreGames = "cross_promo_more_games";
        private CrossPromoPopup _previousPopup;
        private float _animSpeed = 0.25f;
        private Easer _ease = Ease.CubeInOut;

        private void OnEnable()
        {
            Instance = this;
        }

        private void Awake()
        {
            if (Application.isPlaying)
            {
                //---> Invoke Init in 200ms to avoid connection error
                Invoke("Init", 0.2f);
                //Init();
            }
        }

        private void Init()
        {
            CrossPromoLogic = new CrossPromoLogic(CrossPromoManager.Instance.RestAPI);

            CrossPromoLogic.ReceivingProductEvent += CrossPromoLogic_ReceivingProductEvent;

            CrossPromoLogic.Init();
        }

        public void RefreshSkin()
        {
            ui.MoreGamesPopup.RefreshSkin(Skin);
        }

        public void ToggleHorizontalBanner()
        {
            if (ui.BannerImage.IsVisible())
            {
                ui.BannerImage.Hide();
            }
            else
            {
                CrossPromoLogic.BannerRequest();
            }
        }

        #region Show Popups
        public void ShowMoreGamesPopup()
        {
            ui.MoreGamesPopup.Show();
            _previousPopup = ui.MoreGamesPopup;
            AnalyticsManager.Instance.Context = ContextCrossPromoMoreGames;
            AnalyticsManager.Instance.SetCurrentScreen(ui.MoreGamesPopup.PopupName, "click_show_more_games");
        }

        public void ShowInterstitialPopup()
        {
            ui.InterstitialPopup.Show();
            _previousPopup = ui.InterstitialPopup;
            AnalyticsManager.Instance.Context = ContextCrossPromoInterstitial;
            AnalyticsManager.Instance.SetCurrentScreen(ui.InterstitialPopup.PopupName, "click_show_interstitial");
        }

        public void ShowGameDetailsPopup(Product product, string navigationAction)
        {
            AnalyticsManager.Instance.SetCurrentScreen(ui.GameDetailsPopup.PopupName, navigationAction, product);

            if (_previousPopup != null)
                _previousPopup.Hide();

            ui.GameDetailsPopup.Show(product);
        }

        public void ShowMessagePopup(string message)
        {
            ui.MessagePopup.Show(message);

            ui.BackgroundContainer.raycastTarget = true;
            StartCoroutine(Easing.EaseFromTo(0f, 0.75f, _animSpeed, _ease,
            (float value) =>
            {
                ui.BackgroundContainer.color = new Color(0f, 0f, 0f, value);
            }));
        }

        public void HideMessagePopup()
        {
            ui.MessagePopup.Hide();

            StartCoroutine(Easing.EaseFromTo(0.75f, 0f, _animSpeed, _ease,
            (float value) =>
            {
                ui.BackgroundContainer.color = new Color(0f, 0f, 0f, value);
                ui.BackgroundContainer.raycastTarget = false;
            }));
        }
        #endregion

        #region Previous Popup
        public void ShowPreviousPopup(Product fromProduct)
        {
            if (_previousPopup != null)
            {
                AnalyticsManager.Instance.SetCurrentScreen(_previousPopup.PopupName, "click_back", fromProduct);

                _previousPopup.Show();
            }
        }

        public void EmptyPreviousPopup()
        {
            InterstitialPopup interstitial = _previousPopup as InterstitialPopup;
            if (interstitial != null) {
                interstitial.TrackAdDisplayTime();
            }
            _previousPopup = null;
            AnalyticsManager.Instance.Context = null;
        }

        public bool ExistPreviousPopup()
        {
            return _previousPopup != null;
        }

        public bool IsPreviousPopup(CrossPromoPopup popup)
        {
            return _previousPopup == popup;
        }
        #endregion

        #region Background animation
        public void ShowBackground()
        {
            if (!ui.Background.gameObject.activeInHierarchy)
            {
                ui.Background.canvasRenderer.SetAlpha(0.0f);
                ui.Background.gameObject.SetActive(true);
                ui.Background.CrossFadeAlpha(ModalBackgroundAlpha, ModalBackgroundAnimationDuration, true);
            }
        }

        public void HideBackground()
        {
            if (ui.Background.gameObject.activeInHierarchy)
            {
                ui.Background.CrossFadeAlpha(0f, ModalBackgroundAnimationDuration, true);
                Invoke("DeactivateBackground", ModalBackgroundAnimationDuration);
            }
        }

        private void DeactivateBackground()
        {
            ui.Background.gameObject.SetActive(false);
        }
        #endregion

        #region Network events
        private void CrossPromoLogic_ReceivingProductEvent(Product product)
        {
            ui.BannerImage.Init(product);
        }
        #endregion
    }
}
