using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Windows;

public class SUtils : MonoBehaviour
{
    internal Coroutine noticeCor;
    GameObject _rootPanel;
    GameObject _panelNotifyDialog;
    GameObject _textNotify;

    public void Awake()
    {
        GameObject rootCanvas;
        rootCanvas = GameObject.Find("Canvas");
        Debug.Assert(rootCanvas != null);

        _rootPanel = rootCanvas.transform.Find("Panel_Main").gameObject;
        Debug.Assert(_rootPanel != null);

        _panelNotifyDialog = _rootPanel.transform.Find("Panel_Notify_Dialog").gameObject;
        Debug.Assert(_panelNotifyDialog != null);

        _textNotify = _panelNotifyDialog.transform.Find("Text_Notify").gameObject;
        Debug.Assert(_textNotify != null);
    }

    public void showNoticeDialog(string notifyDialogText)
    {
        HideNotice();
        noticeCor = StartCoroutine(NoticeShowScale(notifyDialogText));
    }

    internal void HideNotice()
    {
        if (null != noticeCor)
        {
            StopCoroutine(noticeCor);
            _panelNotifyDialog.SetActive(false);
        }
    }

    IEnumerator NoticeShowScale(string notifyDialogText)
    {
        _textNotify.GetComponent<Text>().text = notifyDialogText;
        _panelNotifyDialog.SetActive(true);
        float targetA = 1;
        float waitForSecondsTime = 4.0f;
        _panelNotifyDialog.GetComponent<CanvasRenderer>().SetAlpha(0);
        _textNotify.GetComponent<CanvasRenderer>().SetAlpha(0);
        float timer = 0;
        while (timer < waitForSecondsTime)
        {
            _panelNotifyDialog.GetComponent<Graphic>().CrossFadeAlpha(targetA, 2, true);
            _textNotify.GetComponent<Graphic>().CrossFadeAlpha(targetA, 2, true);
            yield return new WaitForSeconds(1.0f);
            timer += 1f;
            targetA = targetA == 0 ? 1 : 0;
        }

        _panelNotifyDialog.SetActive(false);
        noticeCor = null;
    }

    public string HandleHumanBitNumber(double size)
    {
        string[] units = new string[] { "B", "KB", "MB", "GB", "TB", "PB"};
        double mod = 1024.0;
        int i = 0;
        while (size >= mod)
        {
            size /= mod;
            i++;
        }
        return System.Math.Round(size) + units[i];
    }

    public string formatDateTime(string secondTime)
    {
        Debug.Log("secondTime  -> " + secondTime);
        if (string.IsNullOrEmpty(secondTime))
            return "剩余： ---";

        float time = float.Parse(secondTime);

        string DateTimes = null;
        float hours = Mathf.Floor((time % (60 * 60 * 24)) / (60 * 60));
        float minutes = Mathf.Floor((time % (60 * 60)) / 60);
        float seconds = Mathf.Floor(time % 60);
        if (hours > 0)
        {
            DateTimes = hours + "小时" + minutes + "分钟" + seconds + "秒";

            return DateTimes;
        }
        else if (minutes > 0)
        {
            DateTimes = minutes + "分钟" + seconds + "秒";

            return DateTimes;
        }
        else
        {
            DateTimes = seconds + "秒";

            return DateTimes;
        }
    }

    //public void OpenDirectory(string path)
    //{
    //    if (string.IsNullOrEmpty(path)) return;

    //    path = path.Replace("/", "\\");
    //    if (!Directory.Exists(path))
    //    {
    //        showNoticeDialog("没有找到" + path + "指定文件夹");
    //        return;
    //    }

    //    System.Diagnostics.Process.Start("explorer.exe", path);
    //}

    public void CopyToClipboard(string input)
    {
#if UNITY_EDITOR
        TextEditor text = new TextEditor();
        text.text = input;
        text.OnFocus();
        text.Copy();
#endif
    }
}
