using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Pos.Dispatcher.Database.Model;
using Pos.Dispatcher.Database.Models;
using RestSharp;

namespace Pos.Dispatcher
{
    public class Synchronize
    {
        private readonly PosDispatcherContext _context;
        private readonly IConfiguration _config;
        private string _apiUrl;
        private string _apiKey;

        public Synchronize(PosDispatcherContext context, IConfiguration config)
        {
            _context = context;
            _config = config;
            _apiUrl = _config["PosApi"];
            _apiKey = _config["ApiKey"];
        }

        public void Go()
        {
            Employee();
            ProductGroup();
            Sale();
        }

        private void Employee()
        {
            List<Employee> employees = _context.Employees.Where(c => c.IsModified == 1).ToList();
            foreach (Employee employee in employees)
            {
                RestClient client = new RestClient(_apiUrl);
                client.AddDefaultHeader("ApiKey", _apiKey);
                var request = new RestRequest("/employee", Method.Post);
                request.AddJsonBody(employee);
                RestResponse restResponse = client.Post(request);
                employee.IsModified = 0;
            }

            _context.SaveChanges();
        }

        private void ProductGroup()
        {
            List<Productgroup> productgroups = _context.Productgroups.Where(c => c.IsModified == 1).ToList();
            foreach (Productgroup product in productgroups)
            {
                RestClient client = new RestClient(_apiUrl);
                client.AddDefaultHeader("ApiKey", _apiKey);
                var request = new RestRequest("/productgroup", Method.Post);
                request.AddJsonBody(product);
                RestResponse response = client.Post(request);
                product.IsModified = 0;
            }
            _context.SaveChanges();
        }

        public void Sale()
        {
            List<Sale> sales = _context.Sales.Where(c => c.IsModified == 1).ToList();
            foreach (Sale sale in sales)
            {
                RestClient client = new RestClient(_apiUrl);
                client.AddDefaultHeader("ApiKey", _apiKey);
                var request = new RestRequest("/sale", Method.Post);
                request.AddJsonBody(sale);
                RestResponse response = client.Post(request);
                sale.IsModified = 0;

                List<SaleLine> saleLines = _context.SaleLines.Where(c => c.SaleId == sale.Id).ToList();
                foreach (SaleLine saleLine in saleLines)
                {
                    client = new RestClient(_apiUrl);
                    client.AddDefaultHeader("ApiKey", _apiKey);
                    request = new RestRequest("/saleline", Method.Post);
                    request.AddJsonBody(saleLine);
                    response = client.Post(request);
                }

                List<Payment> payments = _context.Payments.Where(c => c.SaleId == sale.Id).ToList();
                foreach (Payment payment in payments)
                {
                    client = new RestClient(_apiUrl);
                    client.AddDefaultHeader("ApiKey", _apiKey);
                    request = new RestRequest("/payment", Method.Post);
                    request.AddJsonBody(payment);
                    response = client.Post(request);
                }

                sale.IsModified = 0;
            }
            _context.SaveChanges();
        }
    }
}
