﻿using UnityEngine;

namespace Toast.SmartDownloader.Example
{
    // Workaround crash when exit a standalone program in Windows 7
    public partial class SmartDlExample
    {
        private bool _okExit = false;

        public void OnClickExitOk()
        {
            _okExit = true;
            Application.Quit();
        }

        public void OnClickExitCancel()
        {
            _okExit = false;
            ExitPanel.gameObject.SetActive(false);

            if (_recentResultCode == ResultCode.USER_CANCEL)
            {
                OnClickStartDownload();
            }
        }

#if !UNITY_2018_1_OR_NEWER
        private void OnApplicationQuit()
        {
            if (WantsToQuit() == false)
            {
                Application.CancelQuit();
            }
        }
#endif

        private bool WantsToQuit()
        {
            if (Application.isEditor == true || Application.platform != RuntimePlatform.WindowsPlayer)
            {
                return true;
            }

            bool isQuit = true;

            if (_okExit == false)
            {
                isQuit = false;
            }

            if (ExitPanel.gameObject.activeSelf == false)
            {
                SmartDl.StopDownload();
                ExitPanel.gameObject.SetActive(true);

                isQuit = false;
            }

            return isQuit;
        }
    }
}