﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Toast;
using Toast.SmartDL;

namespace Toast
{
    namespace SmartDL
    {
        public class SmartDLResult
        {
            private int resultCode;
            private bool isCompleted;
            private string resultString;

            // properties
            public int ResultCode { get { return resultCode; } set { resultCode = value; } }
            public bool IsCompleted { get { return isCompleted; } set { isCompleted = value; } }
            public string ResultString { get { return resultString; } set { resultString = value; } }
        }

        public class SmartDLFileStreamInfo
        {
            private int threadId;
            private string fileName;
            private long downloadedBytes;
            private long totalBytes;
            private uint fileNumber;

            // properties
            public int ThreadId { get { return threadId; } set { threadId = value; } }
            public string FileName { get { return fileName; } set { fileName = value; } }
            public long DownloadedBytes { get { return downloadedBytes; } set { downloadedBytes = value; } }
            public long TotalBytes { get { return totalBytes; } set { totalBytes = value; } }
            public uint FileNumber { get { return fileNumber; } set { fileNumber = value; } }
        }

        public class SmartDLProgressInfo
        {
            private Dictionary<int, SmartDLFileStreamInfo> fileMap;
            private float percentage;
            private float speed;
            private long totalReceivedBytes;
            private long totalFileBytes;
            private uint totalFileNumber;

            // constructor
            public SmartDLProgressInfo()
            {
                fileMap = new Dictionary<int, SmartDLFileStreamInfo>();
                percentage = 0;
                speed = 0;
                totalReceivedBytes = 0;
                totalFileBytes = 0;
                totalFileNumber = 0;
            }

            // properties
            public Dictionary<int, SmartDLFileStreamInfo> FileMap { get { return fileMap; } set { fileMap = value; } }
            public float Percentage { get { return percentage; } set { percentage = value; } }
            public float Speed { get { return speed; } set { speed = value; } }
            public long TotalReceivedBytes { get { return totalReceivedBytes; } set { totalReceivedBytes = value; } }
            public long TotalFileBytes { get { return totalFileBytes; } set { totalFileBytes = value; } }
            public uint TotalFileNumber { get { return totalFileNumber; } set { totalFileNumber = value; } }
        }

        public static class SmartDLUnitySkin
        {
            public static void Initialize()
            {
                ChameleonBehaviour.Startup(_ => { });
            }

            public static void SetAppKey(string smartDLAppKey)
            {
                DLCSkin skin = DLCSkin.GetInstance();
                skin.SetAppKey(smartDLAppKey);
            }

            public delegate void StartDownloadCallback(SmartDLResult result);

            public static void StartDownload(string cdnUrl, string metafileName, string downPath, StartDownloadCallback callback)
            {
                DLCSkin skin = DLCSkin.GetInstance();
                skin.StartDownload(cdnUrl, metafileName, downPath, result =>
                {
                    SmartDLResult downloadResult = new SmartDLResult();
                    downloadResult.ResultCode = result.resultCode;
                    downloadResult.IsCompleted = result.isCompleted;
                    downloadResult.ResultString = result.message;
                    callback(downloadResult);
                });
            }

            public static void StopDownload()
            {
                DLCSkin skin = DLCSkin.GetInstance();
                skin.StopDownload();
            }

            public static SmartDLProgressInfo GetProgressInfo()
            {
                DLCSkin skin = DLCSkin.GetInstance();
                var progressInfo = skin.GetProgressInfo();

                SmartDLProgressInfo smartDLProgressInfo = new SmartDLProgressInfo();
                var fileMapCount = progressInfo.fileMap.Count;

                for (int i = 0; i < fileMapCount; i++)
                {
                    SmartDLFileStreamInfo smartDLFileStreamInfo = new SmartDLFileStreamInfo();
                    var fileStreamInfo = progressInfo.fileMap[i];

                    smartDLFileStreamInfo.ThreadId = fileStreamInfo.threadId;
                    smartDLFileStreamInfo.FileName = fileStreamInfo.fileName;
                    smartDLFileStreamInfo.DownloadedBytes = fileStreamInfo.downloadedBytes;
                    smartDLFileStreamInfo.TotalBytes = fileStreamInfo.totalBytes;
                    smartDLFileStreamInfo.FileNumber = fileStreamInfo.fileNumber;

                    smartDLProgressInfo.FileMap[i] = smartDLFileStreamInfo;
                }

                smartDLProgressInfo.Percentage = progressInfo.percentage;
                smartDLProgressInfo.Speed = progressInfo.speed;
                smartDLProgressInfo.TotalReceivedBytes = progressInfo.totalReceivedBytes;
                smartDLProgressInfo.TotalFileBytes = progressInfo.totalFileBytes;
                smartDLProgressInfo.TotalFileNumber = progressInfo.totalFileNumber;

                return smartDLProgressInfo;
            }
        }
    }
}