using System.Diagnostics.Eventing.Reader;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Pos.Api.Database.Data;
using Pos.Api.Database.Models;

namespace Pos.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PosController : Controller
    {
        private readonly PosApiContext _context;
        private readonly IConfiguration _config;

        public PosController(IConfiguration config, PosApiContext context)
        {
            _context = context;
            _config = config;
        }

        //[HttpGet("/update")]
        //public DateTime LastUpdate()
        //{
  
        //    LastUpdate? lastUpdate = _context.LastUpdates.SingleOrDefault();
        //    if (lastUpdate == null)
        //    {
        //        return DateTime.Now;
        //    }

        //    DateTime last = lastUpdate.DateTime.Value;
        //    lastUpdate.DateTime = DateTime.Now;
        //    _context.SaveChanges();

        //    return last;
        //}

        [HttpPost("/employee")]
        public void Employee(Employee employee)
        {
            ValidateApiKey();

            Employee? dbEmployee = _context.Employees.SingleOrDefault(c => c.Id == employee.Id);
            if (dbEmployee == null)
            {
                dbEmployee = new Employee();
                dbEmployee.Id = employee.Id;
                dbEmployee.IsArchived = employee.IsArchived;
                _context.Employees.Add(dbEmployee);
            }
            dbEmployee.Name = employee.Name;

            _context.SaveChanges();
        }

        [HttpPost("/productgroup")]
        public void Product(Productgroup productGroup)
        {
            ValidateApiKey();

            Productgroup dbProductGroup = _context.Productgroups.SingleOrDefault(c => c.Id == productGroup.Id);
            if (dbProductGroup == null)
            {
                dbProductGroup = new Productgroup();
                dbProductGroup.Id = productGroup.Id;
                _context.Productgroups.Add(dbProductGroup);
            }
            dbProductGroup.IsArchived = productGroup.IsArchived;
            dbProductGroup.Name = productGroup.Name;
            dbProductGroup.Index = productGroup.Index;
            _context.SaveChanges();
        }

        [HttpPost("/sale")]
        public void Sale(Sale sale)
        {
            ValidateApiKey();

            Sale? dbSale = _context.Sales.SingleOrDefault(c => c.Id == sale.Id);
            if (dbSale == null)
            {
                dbSale = new Sale();
                dbSale.Id = sale.Id;
                _context.Sales.Add(dbSale);
            }
            dbSale.EmployeeId = sale.EmployeeId;
            dbSale.Time = sale.Time;
            _context.SaveChanges();

        }

        [HttpPost("/saleline")]
        public void SaleLine(SaleLine saleLine)
        {
            ValidateApiKey();

            SaleLine? dbSaleLine = _context.SaleLines.SingleOrDefault(c => c.Id == saleLine.Id);
            if (dbSaleLine == null)
            {
                dbSaleLine = new SaleLine();
                dbSaleLine.Id = saleLine.Id;
                _context.SaleLines.Add(dbSaleLine);
            }
            dbSaleLine.SaleId = saleLine.SaleId;
            dbSaleLine.Price = saleLine.Price;
            dbSaleLine.Pieces = saleLine.Pieces;
            dbSaleLine.Price = saleLine.Price;
            dbSaleLine.Product = saleLine.Product;
            dbSaleLine.Total = saleLine.Total;
            _context.SaveChanges();
        }

        [HttpPost("/payment")]
        public void Payment(Payment payment)
        {
            ValidateApiKey();

            Payment? dbPayment = _context.Payments.SingleOrDefault(c => c.Id == payment.Id);
            if (dbPayment == null)
            {
                dbPayment = new Payment();
                dbPayment.Id = payment.Id;
                _context.Payments.Add(dbPayment);
            }
            dbPayment.Amount = payment.Amount;
            dbPayment.SaleId = payment.SaleId;
            dbPayment.Type = payment.Type;
            _context.SaveChanges();
        }

        private void ValidateApiKey()
        {
            IHeaderDictionary headers = Request.Headers;
            if (!headers.ContainsKey("ApiKey"))
            {
                throw new MissingFieldException("ApiKey missing");
            }

            StringValues apiKeyHeader = headers["ApiKey"];
            string? apiKey = _config["ApiKey"];
            if (!apiKeyHeader.Equals(apiKey))
            {
                throw new MissingFieldException("ApiKey is wrong");
            }

        }

    }
}
