using FruitBank.Common;
using FruitBank.Common.Entities;
using FruitBankHybrid.Shared.Services.SignalRs;

// ReSharper disable CompareOfFloatsByEqualityOperator

namespace FruitBankHybrid.Shared.Tests
{
    [TestClass]
    public sealed class FruitBankPartnerTests
    {
        private const int CustomerIdAasdDsserverCom = 6;//aasd@dsserver.com
        private const string Fixture = "_test.temp";

        private FruitBankSignalRClient _signalRClient = null!;

        private static string GetFixtureName(string name) => $"{GetOriginalName(name)}{Fixture}";
        private static string GetOriginalName(string name) => name.Replace(Fixture, string.Empty);

        [TestInitialize]
        public void TestInit()
        {
            if (!FruitBankConstClient.BaseUrl.Contains("localhost:")) throw new Exception("NEM LOCALHOST-ON TESZTELÜNK!");

            _signalRClient = TestSignalRClientFactory.Create(nameof(FruitBankClientTests));
        }

        #region Partner

        [TestMethod]
        public async Task GetPartnersTest()
        {
            var partners = await _signalRClient.GetPartners();

            Assert.IsNotNull(partners);
            Assert.IsNotEmpty(partners);
        }

        //[TestMethod]
        //[DataRow(1)]
        public async Task<Partner> GetPartnerByIdTest(int partnerId)
        {
            var partner = await _signalRClient.GetPartnerById(partnerId);

            Assert.IsNotNull(partner);
            Assert.AreEqual(partnerId, partner.Id);

            return partner;
        }

        [TestMethod]
        [DataRow(2)]
        public async Task UpdatePartnerTest(int partnerId)
        {
            var partner = await GetPartnerByIdTest(partnerId);

            var newName = GetFixtureName(partner.Name);

            partner.Name = newName;
            partner = await _signalRClient.UpdatePartner(partner);

            Assert.IsNotNull(partner);
            Assert.AreEqual(newName, partner.Name);

            partner.Name = GetOriginalName(partner.Name);
            partner = await _signalRClient.UpdatePartner(partner);
            
            Assert.IsNotNull(partner);
            Assert.AreEqual(partnerId, partner.Id);
        }
        #endregion Partner

        #region CargoPartner
        [TestMethod]
        public async Task GetCargoPartnersTest()
        {
            var partners = await _signalRClient.GetCargoPartners();

            Assert.IsNotNull(partners);
            Assert.IsNotEmpty(partners);
        }
        //[TestMethod]
        //[DataRow(1)]
        public async Task<CargoPartner> GetCargoPartnerByIdTest(int cargoPartnerId)
        {
            var cargoPartner = await _signalRClient.GetCargoPartnerById(cargoPartnerId);

            Assert.IsNotNull(cargoPartner);
            Assert.AreEqual(cargoPartnerId, cargoPartner.Id);

            return cargoPartner;
        }

        [TestMethod]
        [DataRow(1)]
        public async Task UpdateCargoPartnerTest(int cargoPartnerId)
        {
            var cargoPartner = await GetCargoPartnerByIdTest(cargoPartnerId);

            var newName = GetFixtureName(cargoPartner.Name);

            cargoPartner.Name = newName;
            cargoPartner = await _signalRClient.UpdateCargoPartner(cargoPartner);

            Assert.IsNotNull(cargoPartner);
            Assert.AreEqual(newName, cargoPartner.Name);

            cargoPartner.Name = GetOriginalName(cargoPartner.Name);
            cargoPartner = await _signalRClient.UpdateCargoPartner(cargoPartner);
            
            Assert.IsNotNull(cargoPartner);
            Assert.AreEqual(cargoPartnerId, cargoPartner.Id);
        }
        #endregion CargoPartner

        #region CargoTruck
        [TestMethod]
        public async Task GetCargoTrucksTest()
        {
            var cargoTrucks = await _signalRClient.GetCargoTrucks();

            Assert.IsNotNull(cargoTrucks);
            Assert.IsNotEmpty(cargoTrucks);
        }

        //[TestMethod]
        //[DataRow(1)]
        public async Task<CargoTruck> GetCargoTruckByIdTest(int cargoTruckId)
        {
            var cargoTruck = await _signalRClient.GetCargoTruckById(cargoTruckId);

            Assert.IsNotNull(cargoTruck);
            Assert.AreEqual(cargoTruckId, cargoTruck.Id);

            return cargoTruck;
        }

        [TestMethod]
        [DataRow(1)]
        public async Task GetCargoTrucksByCargoPartnerIdTest(int cargoPartnerId)
        {
            var cargoTrucks = await _signalRClient.GetCargoTrucksByCargoPartnerId(cargoPartnerId);

            Assert.IsNotNull(cargoTrucks);
            Assert.IsNotEmpty(cargoTrucks);
        }


        [TestMethod]
        [DataRow(1)]
        public async Task UpdateCargoTruckTest(int cargoTruckId)
        {
            var cargoTruck = await GetCargoTruckByIdTest(cargoTruckId);

            var newLicencePlate = GetFixtureName(cargoTruck.LicencePlate);

            cargoTruck.LicencePlate = newLicencePlate;
            cargoTruck = await _signalRClient.UpdateCargoTruck(cargoTruck);

            Assert.IsNotNull(cargoTruck);
            Assert.AreEqual(newLicencePlate, cargoTruck.LicencePlate);

            cargoTruck.LicencePlate = GetOriginalName(cargoTruck.LicencePlate);
            cargoTruck = await _signalRClient.UpdateCargoTruck(cargoTruck);
            
            Assert.IsNotNull(cargoTruck);
            Assert.AreEqual(cargoTruckId, cargoTruck.Id);
        }
        #endregion CargoTruck

        #region PartnerDepot
        [TestMethod]
        public async Task GetPartnerDepotsTest()
        {
            var partnerDepots = await _signalRClient.GetPartnerDepots();

            Assert.IsNotNull(partnerDepots);
            Assert.IsNotEmpty(partnerDepots);
        }

        //[TestMethod]
        //[DataRow(1)]
        public async Task<PartnerDepot> GetPartnerDepotByIdTest(int partnerDepotId)
        {
            var partnerDepot = await _signalRClient.GetPartnerDepotById(partnerDepotId);

            Assert.IsNotNull(partnerDepot);
            Assert.AreEqual(partnerDepotId, partnerDepot.Id);

            return partnerDepot;
        }

        [TestMethod]
        [DataRow(1)]
        public async Task GetPartnerDepotsByPartnerIdTest(int partnerId)
        {
            var partnerDepots = await _signalRClient.GetPartnerDepotsByPartnerId(partnerId);

            Assert.IsNotNull(partnerDepots);
            Assert.IsNotEmpty(partnerDepots);
        }

        [TestMethod]
        [DataRow(1)]
        public async Task UpdatePartnerDepotTest(int partnerDepotId)
        {
            var partnerDepot = await GetPartnerDepotByIdTest(partnerDepotId);

            var newName = GetFixtureName(partnerDepot.Name);

            partnerDepot.Name = newName;
            partnerDepot = await _signalRClient.UpdatePartnerDepot(partnerDepot);

            Assert.IsNotNull(partnerDepot);
            Assert.AreEqual(newName, partnerDepot.Name);

            partnerDepot.Name = GetOriginalName(partnerDepot.Name);
            partnerDepot = await _signalRClient.UpdatePartnerDepot(partnerDepot);

            Assert.IsNotNull(partnerDepot);
            Assert.AreEqual(partnerDepotId, partnerDepot.Id);
        }
        #endregion PartnerDepot
    }
}
