using UnityEngine;
using System;

namespace Guru.UI
{
    public class GuruRatingController
    {
        private UIRatingView _view;

        private string _supportEmail;
        private bool _showImmediately = false;
        private string _mailTitle;
        private string _mailBody;

        public GuruRatingController()
        {
            
        }


        /// <summary>
        /// 显示 Rating 视图
        /// </summary>
        /// <param name="root">UI挂载根节点(基于UGUI)</param>
        /// <param name="onFeedback">用户操作反馈的回调, 使用默认流程请留空值</param>
        /// <param name="supportEmail">支持邮件地址</param>
        /// <param name="showRatingImmediately">在用户点击操作后, 立即触发响应</param>
        /// <param name="mailTitle">邮件主题</param>
        /// <param name="mailBody">邮件内容</param>
        public void ShowWithData(Transform root, Action<int, string> onFeedback = null, bool showRatingImmediately = true,
            string supportEmail = "", string mailTitle = "", string mailBody = "")
        {
            _supportEmail = supportEmail;
            _mailTitle = mailTitle;
            _mailBody = mailBody;
            _showImmediately = showRatingImmediately;
            
            if (root == null)
            {
                Debug.LogError("UI Root is null, please find a canvas as root");     
                return;
            }
            
            _view = CloneView(root);

            if (onFeedback != null)
            {
                _view.InitWithData(onFeedback); // 使用用户自己的回调
            }
            else
            {
                _view.InitWithData(OnRatingFlow); // 使用默认的回调
            }
        }
        
        private UIRatingView CloneView(Transform root)
        {
            string path = "ui/guru_rating";
            GameObject prefab = Resources.Load<GameObject>(path);
            var go = GameObject.Instantiate(prefab, root);
            go.name = "guru_rating";
            return go.GetComponent<UIRatingView>();
        }
        
        /// <summary>
        /// 默认的操作流程
        /// </summary>
        /// <param name="stars"></param>
        /// <param name="message"></param>
        private void OnRatingFlow(int stars , string message)
        {
            Debug.Log($"OnRatingFlow: stars={stars}, message={message}");
            if (stars == 5)
            {
                // 如果立即显示评价, 则展示IAR
                if (_showImmediately)
                {
                    // 显示商店评价
                    GuruSDK.ShowRating();
                }
            }
            else
            {
                // 不满足4星的话, 会直接发送邮件
                if(string.IsNullOrEmpty(message)) return; // 空信息直接Pass
                if (string.IsNullOrEmpty(_supportEmail)) return;
                
                // 发送邮件
                if(string.IsNullOrEmpty(_mailTitle)) _mailTitle = "Game Feedback";
                if(string.IsNullOrEmpty(_mailBody)) _mailBody = 
                    "Please Enter your message here\n\n\n\n" +
                    "________" +
                    "\n\nPlease Do Not Modify This\n\n" +
                    "Stars: " + stars + "\n\n" +
                    "Model: " + SystemInfo.deviceModel + "\n\n" +
                    "OS: " + SystemInfo.operatingSystem + "\n\n" +
                    "________";
                
                // 直接发送邮件
                GuruSDK.SetEmailFeedback(_supportEmail, _mailTitle, _mailBody);
                Debug.Log($"Send email to:{_supportEmail}, title={_mailTitle},  body={_mailBody}");
            }
        }

    }
}