﻿using System;
using System.IO;
using Toast.SmartDownloader;
using UnityEngine;

namespace Toast.SmartDownloader.Example
{
    public static class SmartDlExampleConfiguration
    {
        private const string APPKEY_PREF_KEY = "com.nhn.smartdl.test.appkey";
        private const string SERVICE_NAME_KEY = "com.nhn.smartdl.test.serviceid";
        private const string DOWNLOAD_PATH_KEY = "com.nhn.smartdl.test.downloadpath";

        private const string RETRY_COUNT_KEY = "com.nhn.smartdl.test.retrycount";
        private const string CONNCECTION_TIMEOUT_KEY = "com.nhn.smartdl.test.connectiontimeout";
        private const string READ_TIMEOUT_KEY = "com.nhn.smartdl.test.readtimeout";

        private const string CHECK_OPTION_KEY = "com.nhn.smartdl.test.checkOption";

        private const string LEAF_DIRECTORY = "SmartDLDownloads";
        private static readonly string SEPERATOR = Path.DirectorySeparatorChar.ToString();

        private static readonly string DEFAULT_SERVICE_NAME = string.Empty;

        private static string DefaultDownloadPath
        {
            get
            {
                return
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + SEPERATOR + LEAF_DIRECTORY;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
                    // MacOS Path : /Users/{USER_NAME}/Documents/SmartDLDownloads
                    Environment.GetFolderPath(Environment.SpecialFolder.Personal) + SEPERATOR + "Documents" + SEPERATOR + LEAF_DIRECTORY;
#else
				Application.persistentDataPath + SEPERATOR + LEAF_DIRECTORY;
#endif
            }
        }

        public static string Appkey { get { return PlayerPrefs.GetString(APPKEY_PREF_KEY, string.Empty); } }
        public static string ServiceName { get { return PlayerPrefs.GetString(SERVICE_NAME_KEY, DEFAULT_SERVICE_NAME); } }
        public static string DownloadPath { get { return PlayerPrefs.GetString(DOWNLOAD_PATH_KEY, DefaultDownloadPath); } }
        public static int RetryCount { get { return PlayerPrefs.GetInt(RETRY_COUNT_KEY, DownloadConfig.Default.RetryDownloadCountPerFile); } }
        public static int ConnectionTimeoutSeconds { get { return PlayerPrefs.GetInt(CONNCECTION_TIMEOUT_KEY, (int)DownloadConfig.Default.DownloadConnectTimeout.TotalSeconds); } }
        public static int ReadTimeoutSeconds { get { return PlayerPrefs.GetInt(READ_TIMEOUT_KEY, (int)DownloadConfig.Default.DownloadReadTimeout.TotalSeconds); } }
        public static int CheckOption { get { return PlayerPrefs.GetInt(CHECK_OPTION_KEY, 0); } }


        public static void SaveAppkey(string appkey) { PlayerPrefs.SetString(APPKEY_PREF_KEY, appkey); }
        public static void SaveServiceName(string serviceName) { PlayerPrefs.SetString(SERVICE_NAME_KEY, serviceName); }
        public static void SaveDownloadPath(string downloadPath) { PlayerPrefs.SetString(DOWNLOAD_PATH_KEY, downloadPath); }
        public static void SaveRetryCount(int retryCount) { PlayerPrefs.SetInt(RETRY_COUNT_KEY, retryCount); }
        public static void SaveConnectionTimeoutSeconds(int connectionTimeoutSeconds) { PlayerPrefs.SetInt(CONNCECTION_TIMEOUT_KEY, connectionTimeoutSeconds); }
        public static void SaveReadTimeoutSeconds(int readTimeoutSeconds) { PlayerPrefs.SetInt(READ_TIMEOUT_KEY, readTimeoutSeconds); }
        public static void SaveCheckOption(int option) { PlayerPrefs.SetInt(CHECK_OPTION_KEY, option); }
    }
}