using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Pixiview.Illust;
using Xamarin.Forms;

namespace Pixiview.Utils
{
    public class IllustResponse<T>
    {
        public bool error;
        public string message;
        public T body;
    }

    public class BookmarkResultData
    {
        public string last_bookmark_id;
        public string stacc_status_id;
    }

    public class Illust
    {
        public string illustId;
        public string illustTitle;
        public string id;
        public string title;
        public int illustType;
        public int xRestrict;
        public string url;
        public string description;
        public string[] tags;
        public string userId;
        public string userName;
        public int width;
        public int height;
        public int pageCount;
        public IllustBookmark bookmarkData;
        public string alt;
        public IllustUrls urls;
        public string seriesId;
        public string seriesTitle;
        public string profileImageUrl;

        public class IllustUrls
        {
            [JsonProperty("250x250")]
            public string x250;
            [JsonProperty("360x360")]
            public string x360;
            [JsonProperty("540x540")]
            public string x540;
        }

        public class IllustBookmark
        {
            public string id;
            [JsonProperty("private")]
            public bool isPrivate;
        }

        public IllustItem ConvertToItem(ImageSource image = null)
        {
            return new IllustItem
            {
                Id = illustId,
                BookmarkId = bookmarkData?.id,
                Title = illustTitle,
                IllustType = (IllustType)illustType,
                Image = image,
                ImageUrl = urls?.x360 ?? url,
                IsRestrict = xRestrict == 1,
                Tags = tags ?? new string[0],
                ProfileUrl = profileImageUrl,
                UserId = userId,
                UserName = userName,
                Width = width,
                Height = height,
                PageCount = pageCount
            };
        }
    }

    public class User
    {
        public string userId;
        public string name;
        public string image;
        public string imageBig;
        public bool premium;
        public bool isFollowed;
        //public string background;
        public int partial;
    }

    public class IllustData : IllustResponse<IllustBody> { }
    public class IllustBody
    {
        public Page page;
        public Thumbnail thumbnails;
        public User[] users;

        public class Page
        {
            public int[] follow;
            public string[] recommend;
            public RecommendByTag[] recommendByTags;
            public Ranking ranking;
            public RecommendUser[] recommendUser;
            public EditorRecommend[] editorRecommend;
            public string[] newPost;

            public class RecommendByTag
            {
                public string tag;
                public string[] ids;
            }

            public class Ranking
            {
                public RankingItem[] items;
                public string date;

                public class RankingItem
                {
                    public string rank;
                    public string id;
                }
            }

            public class RecommendUser
            {
                public int id;
                public string[] illustIds;
            }

            public class EditorRecommend
            {
                public string illustId;
                public string comment;
            }
        }

        public class Thumbnail
        {
            public Illust[] illust;
        }
    }

    public class IllustPreloadBody
    {
        public Dictionary<string, Illust> illust;
        public Dictionary<string, User> user;

        public class Illust
        {
            public string illustId;
            public string illustTitle;
            public string illustComment;
            public string id;
            public string title;
            public string description;
            public int illustType;
            public DateTime createDate;
            public DateTime uploadDate;
            public int xRestrict;
            public IllustUrls urls;
            public IllustTag tags;
            public string alt;
            public string userId;
            public string userName;
            public string userAccount;
            //public Dictionary<string, Illust> userIllusts;
            public int width;
            public int height;
            public int pageCount;
            public int bookmarkCount;
            public int likeCount;
            public int commentCount;
            public int responseCount;
            public int viewCount;
            public bool isOriginal;
            public IllustBookmark bookmarkData;

            public IllustItem CopyToItem(IllustItem item)
            {
                item.BookmarkId = bookmarkData?.id;
                item.Title = illustTitle;
                item.IllustType = (IllustType)illustType;
                item.ImageUrl = urls?.regular;
                item.IsRestrict = xRestrict == 1;
                if (tags != null && tags.tags != null)
                {
                    item.Tags = tags.tags.Where(t => t.locked).Select(t => t.tag).ToArray();
                }
                else
                {
                    item.Tags = new string[0];
                }
                item.UserId = userId;
                item.UserName = userName;
                item.Width = width;
                item.Height = height;
                item.PageCount = pageCount;
                return item;
            }

            [JsonIgnore]
            public string Url => urls.regular;

            public class IllustBookmark
            {
                public string id;
                [JsonProperty("private")]
                public bool isPrivate;
            }

            public class IllustUrls
            {
                public string mini;
                public string thumb;
                public string small;
                public string regular;
                public string original;
            }

            public class IllustTag
            {
                public string authorId;
                public bool isLocked;
                public IllustTagItem[] tags;
                public bool writable;

                public class IllustTagItem
                {
                    public string tag;
                    public bool locked;
                    public bool deletable;
                    public string userId;
                    public IllustTranslate translation;
                    public string userName;

                    public class IllustTranslate
                    {
                        public string en;
                    }
                }
            }
        }
    }

    public class IllustPageData : IllustResponse<IllustPageBody[]> { }
    public class IllustPageBody
    {
        public Urls urls;
        public int width;
        public int height;

        public class Urls
        {
            public string thumb_mini;
            public string small;
            public string regular;
            public string original;
        }
    }

    public class IllustRecommendsData : IllustResponse<IllustRecommendsBody> { }
    public class IllustRecommendsBody
    {
        public Illust[] illusts;
        public string[] nextIds;
    }

    public class IllustUserListData : IllustResponse<IllustUserListBody> { }
    public class IllustUserListBody
    {
        public Dictionary<string, object> illusts;
    }

    public class IllustUserData : IllustResponse<IllustUserBody> { }
    public class IllustUserBody
    {
        public Dictionary<string, Illust> works;
    }

    public class IllustUgoiraData : IllustResponse<IllustUgoiraBody> { }
    public class IllustUgoiraBody
    {
        public string src;
        public string originalSrc;
        public string mime_type;
        public Frame[] frames;

        public class Frame
        {
            public string file;
            public int delay;

            public string FilePath;
            public bool Incompleted;
            public int First;
            public int Last;
            public int Offset;
            public int Length;
        }
    }
}
