using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Google.GData.YouTube;
using Qusion.Cms.Media;
using Qusion.Cms.Media.Core;
using Qusion.Cms.Media.Video;
using Qusion.WebApp.Areas.cms.Models.Libraries;
using Qusion.WebApp.Areas.cms.Models.Videos;
using Qusion.WebApp.Areas.cms.Filters;

namespace Qusion.WebApp.Areas.cms.Controllers
{
    public class VideoLaktasiController : ApiController
    {
        private readonly LibraryService _libraryService;
        private readonly VideoService _videoService;

        public VideoLaktasiController(VideoService videoService, LibraryService libraryService)
        {
            _videoService = videoService;
            _libraryService = libraryService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
      [Compress]
        public HttpResponseMessage Query([FromUri] VideoQueryInput input)
        {
            VideoQuery query = Mapper.Map<VideoQueryInput, VideoQuery>(input);
            if(input.LibraryID != null)
            {
               // query.Sorting.SortOrder;
            }
            Library library = input.LibraryID.HasValue ? _libraryService.GetById(input.LibraryID) : null;

            IList<VideoItem> videos = _videoService.Search(query).ToList();
            var model = new VideoListWrapper
                            {
                                Library = Mapper.Map<Library, LibraryListModel>(library),
                                Videos = Mapper.Map<IEnumerable<VideoItem>, IList<VideoListModel>>(videos),
                                Paging = query.Paging
                            };
            return Request.CreateResponse(HttpStatusCode.OK, model);
        }

      

        /// <summary>
        /// Get one video by its id.
        /// </summary>
        /// <param name="id">Video id.</param>
        /// <returns></returns>
        /// 
       [Compress]
        public HttpResponseMessage Get(Guid id)
        {
            var video = _videoService.GetById(id);

            if(video != null)
            {
                var mapped = Mapper.Map<VideoItem, VideoModel>(video);
                return Request.CreateResponse(HttpStatusCode.OK, mapped);
            }

            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

       
        /// <summary>
        /// Reordering videos in library.
        /// </summary>
        /// <param name="input">Input properties.</param>
        /// <returns>Http status ok.</returns>
        [HttpPost]
        public HttpResponseMessage Organize(OrganizeInput input)
        {
            _videoService.Organize(input.Members);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        [HttpGet]
      [Compress]
        public HttpResponseMessage SearchYoutTube(string q)
        {
            var provider = new YouTubeProvider();
            IEnumerable<VideoItem> result = provider.Search(new YouTubeQuery {Query = q});

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

      
    }
}