﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Admin.Models;
using Admin.ViewModel;
using System.Net.Http;

namespace Admin.Controllers
{
    public class EventController : Controller
    {
        public ActionResult Event()
        {
            EventClient client = new EventClient();
            ViewBag.listEvents = client.findAll();
            return View();
        }

        public ActionResult View1()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(EventViewModel pvm)
        {
            using (var clientUpload = new HttpClient())
            {
                using (var content = new MultipartFormDataContent())
                {
                    byte[] Bytes = new byte[pvm.File.InputStream.Length + 1];
                    pvm.File.InputStream.Read(Bytes, 0, Bytes.Length);
                    var fileContent = new ByteArrayContent(Bytes);
                    fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = pvm.File.FileName };
                    content.Add(fileContent);
                    var requestUri = "http://localhost:52253/api/upload";
                    var result = clientUpload.PostAsync(requestUri, content).Result;
                    if (result.StatusCode == System.Net.HttpStatusCode.Created)
                    {
                        List<string> m = result.Content.ReadAsAsync<List<string>>().Result;
                        ViewBag.Success = m.FirstOrDefault();

                    }
                    else
                    {
                        ViewBag.Failed = "Error !" + result.Content.ToString();
                        return View("Error");
                    }
                }
            }
            ModelState.Clear();
            ModelState.Remove("Event.eventBanner");
            pvm.Event.eventBanner = ((string)ViewBag.Success).Replace("http://localhost:52253/Images/", ""); ;
            EventClient client = new EventClient();
            client.Create(pvm.Event);
            return RedirectToAction("Event");
        }


        public ActionResult Delete(int id)
        {
            EventClient client = new EventClient();
            client.Delete(id);
            return RedirectToAction("Event");
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            EventClient client = new EventClient();
            EventViewModel pvm = new EventViewModel();
            pvm.Event = client.find(id);
            return View("Edit", pvm);
        }

        [HttpPost]
        public ActionResult Edit(EventViewModel pvm)
        {
            if (pvm.File != null)
            {
                using (var clientUpload = new HttpClient())
                {
                    using (var content = new MultipartFormDataContent())
                    {
                        byte[] Bytes = new byte[pvm.File.InputStream.Length + 1];
                        pvm.File.InputStream.Read(Bytes, 0, Bytes.Length);
                        var fileContent = new ByteArrayContent(Bytes);
                        fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = pvm.File.FileName };
                        content.Add(fileContent);
                        var requestUri = "http://localhost:52253/api/upload";
                        var result = clientUpload.PostAsync(requestUri, content).Result;
                        if (result.StatusCode == System.Net.HttpStatusCode.Created)
                        {
                            List<string> m = result.Content.ReadAsAsync<List<string>>().Result;
                            ViewBag.Success = m.FirstOrDefault();

                        }
                        else
                        {
                            ViewBag.Failed = "Error !" + result.Content.ToString();
                            return View("Error");
                        }
                    }
                }
                ModelState.Clear();
                ModelState.Remove("Event.eventBanner");
                pvm.Event.eventBanner = ((string)ViewBag.Success).Replace("http://localhost:52253/Images/", ""); ;
            }
            EventClient client = new EventClient();
            client.Edit(pvm.Event);
            return RedirectToAction("Event");
        }
    }
}