using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Threading;
using Newtonsoft.Json;
using SvetoforVKBot.Models.Updates;
using VkNet;
using VkNet.Model.RequestParams;


namespace SvetoforVKBot.Models.Commands.Admin
{
    public class SelectGetPhotoCommand : Command
    {
        public override string Name => "/getphoto";

        public override void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
            MessagesSendParams @params = new MessagesSendParams();
            var chatId = update.@object.message.from_id;
            string str = "";

            try
            {
                //string[] payload = update.@object.message.text.Split('-');
                string response = "";

                if (chatId != 59111081) return;

                foreach (var att in update.@object.message.attachments)
                {
                    switch (att.type)
                    {
                        case "photo":
                            var usPhoto = client.Photo.GetMessagesUploadServer(peerId: 0);
                            var strArray = att.photo.sizes[att.photo.sizes.Count - 1].url.Split('.');

                            response = UploadFile(
                            serverUrl: usPhoto.UploadUrl,
                            file: att.photo.sizes[att.photo.sizes.Count - 1].url,
                            fileExtension: strArray[strArray.Length - 1]).Result;

                            foreach (var a in client.Photo.SaveMessagesPhoto(response))
                            {

                                str = "{\"id\": " + a.Id +
                                ", \"owner_id\": " + a.OwnerId +
                                ", \"access_key\": \"" + a.AccessKey + "\"}";

                            }
                            break;

                        case "video":
                                str = "{\"id\": " + att.video.id +
                                ", \"owner_id\": " + att.video.owner_id +
                                ", \"access_key\": \"" + att.video.access_key + "\"}";
                            break;
                    }
                }

                @params.Message = str;
                @params.UserId = chatId;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
            catch (Exception ee)
            {
                @params.Message = "‼Ошибка в SelectGetPhotoCommand: " + ee.Message;
                @params.Attachments = null;
                @params.Keyboard = null;
                @params.UserId = 59111081;
                @params.RandomId = GetRandomId();
                client.Messages.SendAsync(@params);
            }
        }

        public override void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
        {
        }
    }
}
