using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using Kendo.Mvc.UI.Fluent;

namespace Kreta.Web.Helpers
{
    public static class FileUploadExtensions
    {
        public static UploadBuilder KretaFileUpload(
            this HtmlHelper helper,
            string name,
            string saveUrl,
            string removeUrl = null,
            bool autoUpload = false,
            bool allowMultiple = false,
            bool showFileList = true,
            string onSuccessEvent = null,
            string onErrorEvent = null,
            string onRemoveEvent = null,
            string onSelectEvent = null,
            string onUploadEvent = null,
            string onCompleteEvent = null,
            string onCancelEvent = null,
            string onProgressEvent = null,
            bool enabled = true,
            bool batchig = true,
            Dictionary<string, object> htmlAttributes = null,
            string uploadText = "Fájl feltöltés",
            string selectText = "Fájl kiválasztása",
            string headerStatusUploadedText = "Állomány feldolgozva, feltöltés folyamatban")
        {
            var upload = helper.Kendo().Upload();
            upload.Name(name);
            upload.Events(e =>
            {
                if (!string.IsNullOrWhiteSpace(onErrorEvent))
                    e.Error(onErrorEvent);
                if (!string.IsNullOrWhiteSpace(onSuccessEvent))
                    e.Success(onSuccessEvent);
                if (!string.IsNullOrWhiteSpace(onRemoveEvent))
                    e.Remove(onRemoveEvent);
                if (!string.IsNullOrWhiteSpace(onSelectEvent))
                    e.Select(onSelectEvent);
                if (!string.IsNullOrWhiteSpace(onUploadEvent))
                    e.Upload(onUploadEvent);
                if (!string.IsNullOrWhiteSpace(onCompleteEvent))
                    e.Complete(onCompleteEvent);
                if (!string.IsNullOrWhiteSpace(onCancelEvent))
                    e.Cancel(onCancelEvent);
                if (!string.IsNullOrWhiteSpace(onProgressEvent))
                    e.Progress(onProgressEvent);
            });
            upload.Enable(enabled);
            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary<string, object>();
            }

            htmlAttributes.Add("title", selectText);
            upload.HtmlAttributes(htmlAttributes);
            upload.Multiple(allowMultiple);
            upload.ShowFileList(showFileList);
            upload.Async(a =>
            {
                a.AutoUpload(autoUpload);
                a.SaveUrl(saveUrl);
                a.RemoveUrl(removeUrl);
                a.Batch(batchig);
            });
            upload.Messages(m =>
            {
                m.Select(selectText);
                m.Cancel("Mégse");
                m.DropFilesHere("Húzza a kívánt fájlt ide");
                m.HeaderStatusUploaded(headerStatusUploadedText);
                m.HeaderStatusUploading("Feldolgozás alatt");
                m.Remove("Fájl törlése");
                m.Retry("Újrapróbálkozás");
                m.StatusFailed("Sikertelen");
                m.StatusUploaded("Sikeres");
                m.StatusUploading("Feldolgozás folyamatban");
                m.UploadSelectedFiles(uploadText);
            });
            return upload;
        }

        public static MvcHtmlString KretaFileUpload(
            this HtmlHelper helper,
            string name,
            string onSelectEvent = null,
            string onRemoveEvent = null,
            bool allowMultiple = true,
            bool needCustomHtmlString = false,
            string selectText = "Fájl tallózása...",
            Dictionary<string, object> htmlAttributes = null)
        {
            var upload = helper.Kendo().Upload();
            upload.Name(name);
            upload.Events(e =>
            {
                if (!string.IsNullOrWhiteSpace(onSelectEvent))
                    e.Select(onSelectEvent);
                if (!string.IsNullOrWhiteSpace(onRemoveEvent))
                    e.Remove(onRemoveEvent);
            });
            if (htmlAttributes != null)
            {
                upload.HtmlAttributes(htmlAttributes);
            }

            upload.Messages(m =>
            {
                m.Select(selectText);
                m.Cancel("Mégse");
                m.DropFilesHere("Húzza a kívánt fájlt ide");
                m.HeaderStatusUploaded("Állomány feldolgozva, feltöltés folyamatban");
                m.HeaderStatusUploading("Feldolgozás alatt");
                m.Remove("Fájl törlése");
                m.Retry("Újrapróbálkozás");
                m.StatusFailed("Sikertelen");
                m.StatusUploaded("Sikeres");
                m.StatusUploading("Feldolgozás folyamatban");
                m.UploadSelectedFiles("Fájl feltöltés");
            });

            upload.Multiple(allowMultiple);

            var htmlString = upload.ToString();
            if (needCustomHtmlString)
            {
                Match match = Regex.Match(htmlString, @"(.*\/>)(.)", RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    var inputHtmlString = match.Groups[1].Value;
                    var customHtmlString = string.Empty;
                    var scriptHtmlString = htmlString.Substring(match.Groups[2].Index);

                    htmlString = inputHtmlString + customHtmlString + scriptHtmlString;
                }
            }
            return new MvcHtmlString(htmlString);
        }

        public static MvcHtmlString KretaAsyncFileUpload(
            this HtmlHelper helper,
            string name,
            // Ez azért kell ide, mert ha nincs megadva, akkor hiába van ott az async, nem lesz aszinkron a komponens. Ez később felül van csapva a megfelelő url-lel
            string saveUrl = "http://localhost",
            string onSelectEvent = null,
            string onUploadEvent = null,
            string onErrorEvent = null,
            string onCompleteEvent = null,
            bool allowMultiple = true,
            bool needCustomHtmlString = false,
            string selectText = "Fájl tallózása...",
            Dictionary<string, object> htmlAttributes = null
            )
        {
            var upload = helper.Kendo().Upload();
            upload.Name(name);
            upload.Events(e =>
            {
                if (!string.IsNullOrWhiteSpace(onSelectEvent))
                    e.Select(onSelectEvent);
                if (!string.IsNullOrWhiteSpace(onErrorEvent))
                    e.Error(onErrorEvent);
                if (!string.IsNullOrWhiteSpace(onUploadEvent))
                    e.Upload(onUploadEvent);
                if (!string.IsNullOrWhiteSpace(onCompleteEvent))
                    e.Complete(onCompleteEvent);
            });

            upload.Async(a =>
            {
                a.AutoUpload(true);
                a.SaveUrl(saveUrl);
            });

            if (htmlAttributes != null)
            {
                upload.HtmlAttributes(htmlAttributes);
            }

            upload.Messages(m =>
            {
                m.Select(selectText);
                m.HeaderStatusUploaded("Kész");
                m.HeaderStatusUploading("Feldolgozás alatt");
            });

            upload.Multiple(allowMultiple);
            upload.ShowFileList(false);

            var htmlString = upload.ToString();
            if (needCustomHtmlString)
            {
                Match match = Regex.Match(htmlString, @"(.*\/>)(.)", RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    var inputHtmlString = match.Groups[1].Value;
                    var customHtmlString = string.Empty;
                    var scriptHtmlString = htmlString.Substring(match.Groups[2].Index);

                    htmlString = inputHtmlString + customHtmlString + scriptHtmlString;
                }
            }

            return new MvcHtmlString(htmlString);
        }
    }
}