using Dapper;
using ManagementApp.Dapper.AExtentions;
using ManagementApp.DBModels;
using Microsoft.AspNetCore.Mvc;
using MySqlConnector;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;

namespace ManagementApp.Models
{
    public class TypeStorageServerModel: ModelBase, IPaging
    {
        public int ID { set; get; }
        public string Name { get; set; }

        public override async Task<MessageHeader> AddAsync()
        {
            var f = new MessageHeader();
            try
            {
                using (var con = new MySqlConnection(DBManagement.GetConnectionString()))
                {
                    await con.OpenAsync();
                    await con.Insert(new TypeStorageServer { TypeName = Name });
                }
                f.Status = 1;
                f.Message = "OK";
            }
            catch(DbException ex)
            {
                f.Status = 0;
                f.Message = ex.Message;
                f.ID = 1001;
            }
            return f;
        }

        public override Task<MessageHeader> DeleteAsync()
        {
            throw new NotImplementedException();
        }

        public override async Task<MessageHeader> UpdateAsync()
        {
            var f = new MessageHeader();
            try
            {
                using (var con = new MySqlConnection(DBManagement.GetConnectionString()))
                {
                    await con.OpenAsync();
                    await con.Update(new TypeStorageServer { IdTypeStorageServer = ID, TypeName = Name });
                }
                f.Status = 1;
                f.Message = "OK";
            }
            catch (DbException ex)
            {
                f.Status = 0;
                f.Message = ex.Message;
                f.ID = 1002;
            }
            return f;
        }

        public async Task<IActionResult> GetTypeStoragesAsync()
        {
            using (var con = new MySqlConnection(DBManagement.GetConnectionString()))
            {
                await con.OpenAsync();
                if (IsFirstQuery)
                {
                    using (var multi =  await con
                    .QueryMultipleAsync("SELECT COUNT(*) FROM TypeStorageServer;" +
                                        "SELECT * FROM TypeStorageServer ORDER BY idTypeStorageServer DESC " +
                                        "OFFSET @Offset  ROWS FETCH NEXT @PageSize ROWS ONLY", new { Offset = (PageNumber - 1) * PageSize, PageSize = this.PageSize })) 
                    {
                        int maxRow = await multi.ReadSingleAsync<int>(); 
                        IEnumerable<TypeStorageServer> types = await multi.ReadAsync<TypeStorageServer>();
                        return new JsonResult(new
                        {
                            mrows = maxRow,
                            data = JsonConvert.SerializeObject(types)
                        });
                    }
                }
                else
                {
                    IEnumerable<TypeStorageServer> t = (await con
                    .QueryAsync<TypeStorageServer>("SELECT * FROM TypeStorageServer ORDER BY idTypeStorageServer DESC " +
                                                   "OFFSET @Offset  ROWS FETCH NEXT @PageSize ROWS ONLY", new { Offset = (PageNumber - 1) * PageSize, PageSize = this.PageSize }));
                    return new JsonResult(new
                    {
                        data = JsonConvert.SerializeObject(t)
                    });
                }
            }
        }

        public static async Task<IActionResult> GetAllTypeStorage()
        {
            using (var con = new MySqlConnection(DBManagement.GetConnectionString()))
            {
                IEnumerable<dynamic> t = await con
                   .QueryAsync("SELECT idTypeStorageServer AS ID, TypeName AS Type FROM TypeStorageServer ORDER BY idTypeStorageServer ASC");
                return new JsonResult(new
                {
                    data = JsonConvert.SerializeObject(t)
                });
            }
        }
    }
}
