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

namespace ManagementApp.Models
{
    public class ValidationDomainModel: ModelBase
    {
        public int ID { set; get; }

        public int PortNumber { set; get; }

        public string Protocol { set; get; }

        public string Name { set; get; }

        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 ValidationDomain { DomainName = Name, PortNumber = this.PortNumber, Protocol = this.Protocol });
                }
                f.Status = 1;
                f.Message = "OK";
            }
            catch (DbException ex)
            {
                f.Status = 0;
                f.Message = ex.Message;
                f.ID = 61031;
            }
            return f;
        }
        public async Task<IActionResult> GetValidationDomainAsync()
        {
            using (var con = new MySqlConnection(DBManagement.GetConnectionString()))
            {
                await con.OpenAsync();
                if (IsFirstQuery)
                {
                    using (var multi = await con
                    .QueryMultipleAsync("SELECT COUNT(*) FROM ValidationDomain;" +
                                        "SELECT * FROM ValidationDomain ORDER BY idValidationDomain DESC " +
                                        "OFFSET @Offset  ROWS FETCH NEXT @PageSize ROWS ONLY", new { Offset = (PageNumber - 1) * PageSize, PageSize = this.PageSize }))
                    {
                        int maxRow = await multi.ReadSingleAsync<int>();
                        IEnumerable<ValidationDomain> types = await multi.ReadAsync<ValidationDomain>();
                        return new JsonResult(new
                        {
                            mrows = maxRow,
                            data = JsonConvert.SerializeObject(types)
                        });
                    }
                }
                else
                {
                    IEnumerable<ValidationDomain> t = (await con
                    .QueryAsync<ValidationDomain>("SELECT * FROM ValidationDomain ORDER BY idValidationDomain 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 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 ValidationDomain { IdValidationDomain = ID, DomainName = Name, PortNumber = this.PortNumber, Protocol = this.Protocol });
                }
                f.Status = 1;
                f.Message = "OK";
            }
            catch (DbException ex)
            {
                f.Status = 0;
                f.Message = ex.Message;
                f.ID = 61032;
            }
            return f;
        }

        public static async Task<IActionResult> GetAllValidationDomain()
        {
            using (var con = new MySqlConnection(DBManagement.GetConnectionString()))
            {
                await con.OpenAsync();
                IEnumerable<dynamic> t = (await con
                              .QueryAsync("SELECT IDValidationDomain As ID, DomainName As Name  FROM ValidationDomain ORDER BY idValidationDomain DESC"));
                return new JsonResult(new
                {
                    data = JsonConvert.SerializeObject(t)
                });
            }
        }
    }
}
