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

namespace Toast.SmartDownloader.Example
{
    public static class SmartDlExampleConfiguration
    {
        private const string AppkeyPrefKey = "com.nhn.smartdl.test.appkey";
        private const string ServiceNameKey = "com.nhn.smartdl.test.serviceid";
        private const string DownloadPathKey = "com.nhn.smartdl.test.downloadpath";

        private const string RetryCountKey = "com.nhn.smartdl.test.retrycount";
        private const string ConnectionTimeoutKey = "com.nhn.smartdl.test.connectiontimeout";
        private const string ReadTimeoutKey = "com.nhn.smartdl.test.readtimeout";

        private const string LeafDirectory = "SmartDLDownloads";
        private static readonly string Seperator = Path.DirectorySeparatorChar.ToString();

        private static readonly string DefaultServiceName = string.Empty;

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

        public static string Appkey { get { return PlayerPrefs.GetString(AppkeyPrefKey, string.Empty); } }
        public static string ServiceName { get { return PlayerPrefs.GetString(ServiceNameKey, DefaultServiceName); } }
        public static string DownloadPath { get { return PlayerPrefs.GetString(DownloadPathKey, DefaultDownloadPath); } }
        public static int RetryCount { get { return PlayerPrefs.GetInt(RetryCountKey, DownloadConfig.Default.RetryDownloadCountPerFile); } }
        public static int ConnectionTimeoutSeconds { get { return PlayerPrefs.GetInt(ConnectionTimeoutKey, (int)DownloadConfig.Default.DownloadConnectTimeout.TotalSeconds); } }
        public static int ReadTimeoutSeconds { get { return PlayerPrefs.GetInt(ReadTimeoutKey, (int)DownloadConfig.Default.DownloadReadTimeout.TotalSeconds); } }


        public static void SaveAppkey(string appkey) { PlayerPrefs.SetString(AppkeyPrefKey, appkey); }
        public static void SaveServiceName(string serviceName) { PlayerPrefs.SetString(ServiceNameKey, serviceName); }
        public static void SaveDownloadPath(string downloadPath) { PlayerPrefs.SetString(DownloadPathKey, downloadPath); }
        public static void SaveRetryCount(int retryCount) { PlayerPrefs.SetInt(RetryCountKey, retryCount); }
        public static void SaveConnectionTimeoutSeconds(int connectionTimeoutSeconds) { PlayerPrefs.SetInt(ConnectionTimeoutKey, connectionTimeoutSeconds); }
        public static void SaveReadTimeoutSeconds(int readTimeoutSeconds) { PlayerPrefs.SetInt(ReadTimeoutKey, readTimeoutSeconds); }
    }
}