﻿using UnityEngine;
using UnityEngine.UI;
using TentuPlay.Api;
using LitJson;

namespace TentuPlay.CRM
{
    // THIS SCRIPT IS ATTACHED TO TentuPlayManualOffer_landscape and TentuPlayManualOffer_portrait.
    public class TentuPlayManualOffer: MonoBehaviour
    {
        public TPOfferCalculated thisOffer;
        private string thisPlayerUUID;
        private string thisLanguage;
        public bool is_dont_show_today_checked;
        
        [SerializeField]
        Toggle DoNotShowToday;

        public int PlaceOffer(TPOfferCalculated my_offer, string player_id, string language)
        {
            thisPlayerUUID = player_id;
            thisOffer = my_offer;
            thisLanguage = language;

            TPStashEvent myStashEvent = new TPStashEvent();
            myStashEvent.StashOfferEvent(player_uuid: player_id, offer_id: my_offer.offer_id, message_status: messageStatus.Open);

            TPPersonalizedOffer myPerOffer = new TPPersonalizedOffer();
            myPerOffer.OfferOpened(my_offer.offer_id, (response) =>
            {
            });

            // Default is false.
            // Or you can set the default value as saved value(thisOffer.checked_dont_show_today)
            is_dont_show_today_checked = false;
            try
            {
                gameObject.GetComponent<MessageController>().Show();
            }
            catch
            {
                return -1;
            }
            return 1;
        }

        public void GoToOfferEvent()
        {
            // Log the Offer interact event
            int r = new TPStashEvent().StashOfferEvent(
                player_uuid: thisPlayerUUID, offer_id: thisOffer.offer_id, message_status: messageStatus.Interact); 
            new TPUploadData().UploadData(toCheckInterval: false);

            JsonData offer = JsonMapper.ToObject(thisOffer.offer);
            if (offer.ContainsKey("message"))
            {
                if (offer["message"].ContainsKey(thisLanguage))
                {
                    if (offer["message"][thisLanguage].ContainsKey("url"))
                    {
                        string url = (string)offer["message"][thisLanguage]["url"];
                        Application.OpenURL(url);
                    }
                    else return;
                }
                else
                {
                    if (TPSettings.DEBUG) Debug.Log("<color=red>TPError</color>||Key Error. Check your language parameter.");
                    return;
                }
            }
            else return;

            if (is_dont_show_today_checked)
                new TPPersonalizedOffer().MarkDoNotShowToday(thisOffer.offer_id);
            else
                new TPPersonalizedOffer().UnmarkDoNotShowToday(thisOffer.offer_id);

            OnGoToOfferEvent();

#if USE_DEMO
            // Select(refresh) offer lists when offer is closed.
            // Use this code for ShingGoong demo game.
            TPPersonalizedOffer myPO = new TPPersonalizedOffer();
            foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
            { 
                if (go.name == "Player")
                    go.GetComponent<TentuPlayCRMPlayerController>().myOffers = myPO.SelectOfferInfo(thisPlayerUUID, "en");
            }
            GameObject.Find("MailBoxOpen_offer").GetComponent<MailBoxOpen_offer>().ListOffers();
#endif
        }

        public void CloseOfferEvent()
        {
            // Log the Offer close event
            int r = new TPStashEvent().StashOfferEvent(
                player_uuid: thisPlayerUUID, offer_id: thisOffer.offer_id, message_status: messageStatus.Dismiss); 
            new TPUploadData().UploadData(toCheckInterval: false);

            if (is_dont_show_today_checked)
                new TPPersonalizedOffer().MarkDoNotShowToday(thisOffer.offer_id);
            else
                new TPPersonalizedOffer().UnmarkDoNotShowToday(thisOffer.offer_id);
            
            OnCloseOfferEvent();

#if USE_DEMO
            // Select(refresh) offer lists when offer is closed.
            // Use this code for ShingGoong demo game.
            TPPersonalizedOffer myPO = new TPPersonalizedOffer();
            foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
            { 
                if (go.name == "Player")
                    go.GetComponent<TentuPlayCRMPlayerController>().myOffers = myPO.SelectOfferInfo(thisPlayerUUID, "en");
            }
            GameObject.Find("MailBoxOpen_offer").GetComponent<MailBoxOpen_offer>().ListOffers();
#endif
        }

        public void CheckDoNotShowToday()
        {
            if (DoNotShowToday.isOn)
                is_dont_show_today_checked = true;
            else
                is_dont_show_today_checked = false;
        }

        protected virtual void OnGoToOfferEvent()
        {
            gameObject.GetComponent<MessageController>().Close();
        }

        protected virtual void OnCloseOfferEvent()
        {
            gameObject.GetComponent<MessageController>().Close();
        }
    }
}
