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

public static class SmartDlExampleConfiguration
{
    private const string AppkeyPrefKey = "com.nhnent.smartdl.test.appkey";
    private const string ServiceNameKey = "com.nhnent.smartdl.test.serviceid";
    private const string DownloadPathKey = "com.nhnent.smartdl.test.downloadpath";

    private const string RetryCountKey = "com.nhnent.smartdl.test.retrycount";
    private const string ConnectionTimeoutKey = "com.nhnent.smartdl.test.connectiontimeout";
    private const string ReadTimeoutKey = "com.nhnent.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); }
}
