using Google.Apis.YouTube.v3.Data;
using System;
using System.Collections.Generic;
using static Google.Apis.YouTube.v3.SearchResource.ListRequest;

namespace FoxTube
{
    public class SearchParameters
    {
        public class Filters
        {
            public static class Enumerations
            {
                public enum Order { Relevance, Date, Views, Rating, Title }
                public enum Type { All, Video, Channel, Playlist }
                public enum Date { Any, Hour, Today, Week, Month, Year }
                public enum Duration { Any, Long, Medium, Short }
                public enum ConversionType { Order, Type, Date, Duration, HD, ThreeD, Captions, LiveEvent, CreativeCommons }
            }

            public Enumerations.Order Order { get; set; } = Enumerations.Order.Relevance;
            public Enumerations.Date Date { get; set; } = Enumerations.Date.Any;
            public Enumerations.Type Type { get; set; } = Enumerations.Type.All;
            public Enumerations.Duration Duration { get; set; } = Enumerations.Duration.Any;
            public bool HD { get; set; } = false;
            public bool Is3D { get; set; } = false;
            public bool Captions { get; set; } = false;
            public bool LiveEvent { get; set; } = false;
            public bool CreativeCommons { get; set; } = false;

            public object GetParameter(Enumerations.ConversionType type)
            {
                switch(type)
                {
                    case Enumerations.ConversionType.Captions:
                        if (Captions)
                            return VideoCaptionEnum.ClosedCaption;
                        else return VideoCaptionEnum.Any;
                    case Enumerations.ConversionType.CreativeCommons:
                        if (CreativeCommons)
                            return VideoLicenseEnum.CreativeCommon;
                        else return VideoLicenseEnum.Any;
                    case Enumerations.ConversionType.Date:
                        switch(Date)
                        {
                            case Enumerations.Date.Any:
                                return null;
                            case Enumerations.Date.Hour:
                                return DateTime.Now.Subtract(TimeSpan.FromHours(1));
                            case Enumerations.Date.Today:
                                return DateTime.Now.Subtract(TimeSpan.FromDays(1));
                            case Enumerations.Date.Week:
                                return DateTime.Now.Subtract(TimeSpan.FromDays(7));
                            case Enumerations.Date.Month:
                                return DateTime.Now.Subtract(TimeSpan.FromDays(31));
                            case Enumerations.Date.Year:
                                return DateTime.Now.Subtract(TimeSpan.FromDays(365));
                        }
                        break;
                    case Enumerations.ConversionType.Duration:
                        return (VideoDurationEnum)Duration;
                    case Enumerations.ConversionType.HD:
                        if (HD)
                            return VideoDefinitionEnum.High;
                        else return VideoDefinitionEnum.Any;
                    case Enumerations.ConversionType.LiveEvent:
                        if (LiveEvent)
                            return EventTypeEnum.Live;
                        else return null;
                    case Enumerations.ConversionType.Order:
                        Dictionary<int, int> d = new Dictionary<int, int>()
                        {
                            { 0, 2 },
                            { 1, 0 },
                            { 2, 5 },
                            { 3, 1 },
                            { 4, 3 }
                        };
                        return (OrderEnum)d[(int)Order];
                    case Enumerations.ConversionType.ThreeD:
                        if (Is3D)
                            return VideoDimensionEnum.Value3d;
                        else return VideoDimensionEnum.Any;
                    case Enumerations.ConversionType.Type:
                        switch(Type)
                        {
                            case Enumerations.Type.All:
                                return "video,channel,playlist";
                            case Enumerations.Type.Channel:
                                return "channel";
                            case Enumerations.Type.Playlist:
                                return "playlist";
                            case Enumerations.Type.Video:
                                return "video";
                        }
                        break;
                }
                return null;
            }
        }

        public string Term { get; private set; }
        public string Channel { get; private set; }
        public VideoCategory Category { get; set; }
        public Filters Filter { get; private set; } = new Filters();

        public SearchParameters(string term)
        {
            Term = term;
        }

        public SearchParameters(VideoCategory category)
        {
            Category = category;
            Filter = new Filters();
            Filter.Type = Filters.Enumerations.Type.Video;
        }

        public SearchParameters(string term, Filters filters)
        {
            Term = term;
            Filter = filters;
        }

        public SearchParameters(string term, string channelId)
        {
            Term = term;
            Channel = channelId;
        }


        public SearchParameters(string term, string channelId, Filters filters)
        {
            Term = term;
            Channel = channelId;
            Filter = filters;
        }

        public override string ToString()
        {
            return $@"Term: {Term}
Channel id: {Channel}
Category id: {Category}
Filters:
    Order: {Filter.Order}
    Type: {Filter.Type}
    Date: {Filter.Date}
    Duration: {Filter.Duration}
    HD: {Filter.HD}
    3D: {Filter.Is3D}
    Event type: {Filter.LiveEvent}
    CC: {Filter.Captions}
    License: {Filter.CreativeCommons}";
        }
    }
}
