﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Net;
using System.IO;

namespace Shblitz.Common
{
    public class Downloader
    {
        WebClient    m_WebClient;
        DownloadKind m_DownloadKind;
        Uri          m_DownloadUri;

        /// <summary>
        /// 다운로드 완료 이벤트
        /// </summary>
        public event DownloadCompleteEventHandler DownloadComplete;
        private void OnDownloadComplete(DownloadCompleteEventArgs e)
        {
            if (this.DownloadComplete != null)
            {
                this.DownloadComplete(e);
            }
        }

        /// <summary>
        /// 다운로드 에러 이벤트
        /// </summary>
        public event DownloadErrorEventHandler DownloadError;
        private void OnDownloadError(DownloadErrorEventArgs e)
        {
            if (this.DownloadError != null)
            {
                this.DownloadError(e);
            }
        }

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="downloadUri"></param>
        /// <param name="kind"></param>
        public Downloader(Uri downloadUri, DownloadKind kind)
        {
            m_WebClient = new WebClient();

            m_DownloadKind = kind;
            m_DownloadUri  = downloadUri;

            if (m_DownloadKind == DownloadKind.Binary)
            {
                m_WebClient.OpenReadCompleted += new OpenReadCompletedEventHandler(m_WebClient_OpenReadCompleted);
            }
            else
            {
                m_WebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(m_WebClient_DownloadStringCompleted);
            }
        }

        /// <summary>
        /// 다운로드 시작
        /// </summary>
        public void Start()
        {
            if (m_DownloadKind == DownloadKind.Binary)
            {
                m_WebClient.OpenReadAsync(m_DownloadUri);
            }
            else
            {
                m_WebClient.DownloadStringAsync(m_DownloadUri);
            }
        }

        /// <summary>
        /// 바이너리 파일 다운로드 완료 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_WebClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if ((e.Error == null) && (e.Cancelled == false))
            {
                OnDownloadComplete(new DownloadCompleteEventArgs(e.Result));
            }
            else
            {
                OnDownloadError(new DownloadErrorEventArgs(e.Error.Message));
            }
        }

        /// <summary>
        /// 텍스트 파일 다운로드 완료 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void m_WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if(e.Error == null)
            {
                OnDownloadComplete(new DownloadCompleteEventArgs(e.Result));
            }
            else
            {
                OnDownloadError(new DownloadErrorEventArgs(e.Error.Message));
            }
        }
    }

    /// <summary>
    /// 다운로드 구분
    /// </summary>
    public enum DownloadKind
    {
        Text,
        Binary
    }

    /// <summary>
    /// 다운로드 완료 이벤트
    /// </summary>
    /// <param name="e"></param>
    public delegate void DownloadCompleteEventHandler(DownloadCompleteEventArgs e);
    public class DownloadCompleteEventArgs : EventArgs
    {
        public Stream ResultStream { get; set; }
        public string ResultString { get; set; }

        public DownloadCompleteEventArgs(Stream result)
        {
            this.ResultStream = result;
        }

        public DownloadCompleteEventArgs(string result)
        {
            this.ResultString = result;
        }
    }

    /// <summary>
    /// 다운로드 에러 이벤트
    /// </summary>
    /// <param name="e"></param>
    public delegate void DownloadErrorEventHandler(DownloadErrorEventArgs e);
    public class DownloadErrorEventArgs : EventArgs
    {
        public string Error { get; set; }

        public DownloadErrorEventArgs(string error)
        {
            this.Error = error;
        }
    }
}

