using Hcs.Client;
using Hcs.Client.Api.Payload.HouseManagement;
using Hcs.Client.Api.Registry;
using Hcs.Service.Async.HouseManagement;
using System;
using System.Linq;

namespace Hcs.TestApp.Scenario
{
    internal class HouseManagementScenario(UniClient client)
    {
        private readonly UniClient client = client;

        internal void ExportAccount()
        {
            var result = client.HouseManagement.ExportAccountAsync("c48e5025-f77a-494f-8aab-f773da623a25").Result;
            Console.WriteLine("Scenario execution " + (result != null && result.Count() > 0 ? "succeeded" : "failed"));
        }

        internal void ExportHouse()
        {
            var result = client.HouseManagement.ExportHouseAsync("c48e5025-f77a-494f-8aab-f773da623a25").Result;
            Console.WriteLine("Scenario execution " + (result != null && result.Count() > 0 ? "succeeded" : "failed"));
        }

        internal void ExportAllSupplyResourceContractData()
        {
            var result = client.HouseManagement.ExportSupplyResourceContractDataAsync().Result;
            Console.WriteLine("Scenario execution " + (result != null && result.Count() > 0 ? "succeeded" : "failed"));
        }

        internal void ExportSupplyResourceContractDataByGuid()
        {
            var result = client.HouseManagement.ExportSupplyResourceContractDataAsync(Guid.Parse("575a7ff9-5473-4ab4-b077-fa80c1f85f0b")).Result;
            Console.WriteLine("Scenario execution " + (result != null ? "succeeded" : "failed"));
        }

        internal void ExportSupplyResourceContractDataByNumber()
        {
            var result = client.HouseManagement.ExportSupplyResourceContractDataAsync("239009043").Result;
            Console.WriteLine("Scenario execution " + (result != null ? "succeeded" : "failed"));
        }

        internal void ExportSupplyResourceContractObjectAddressData()
        {
            var result = client.HouseManagement.ExportSupplyResourceContractObjectAddressDataAsync(Guid.Parse("575a7ff9-5473-4ab4-b077-fa80c1f85f0b")).Result;
            Console.WriteLine("Scenario execution " + (result != null ? "succeeded" : "failed"));
        }

        internal void ImportAccountData()
        {
            var payload = new ImportAccountDataPayload()
            {
                accountNumber = "458938932489324",
                accountReasons = new AccountReasonsImportType()
                {
                    SupplyResourceContract =
                    [
                        new()
                        {
                            Items = ["2fb693f0-3ac8-4a9a-af0e-cb6515d61370"],
                            ItemsElementName = [ItemsChoiceType17.ContractGUID]
                        }
                    ]
                },
                accountType = ImportAccountDataPayload.AccountType.RSO,
                livingPersonsNumber = 1,
                totalSquare = 29.3m,
                residentialSquare = 29.3m,
                heatedArea = 29.3m,
                accomodations =
                [
                    new()
                    {
                        Item = "6e011824-3d8a-48d7-b0ee-7b36be7096bf",
                        ItemElementName = ItemChoiceType9.PremisesGUID
                    }
                ],
                payerInfo = new AccountTypePayerInfo()
                {
                    IsRenter = true,
                    IsRenterSpecified = true,
                    Item = new AccountIndType()
                    {
                        Item = "26041127927",
                        FirstName = "Имя",
                        Surname = "Фамилия",
                        Patronymic = "Отчество"
                    }
                }
            };
            var result = client.HouseManagement.ImportAccountDataAsync(payload).Result;
            Console.WriteLine("Scenario execution " + (result ? "succeeded" : "failed"));
        }

        internal void ImportContractData()
        {
            var signingDate = new DateTime(2017, 01, 01);
            var effectiveDate = new DateTime(2017, 01, 01);
            var planComptetionDate = new DateTime(2078, 12, 31);

            var payload = new ImportContractDataPayload()
            {
                contractObjects = [new importContractRequestContractPlacingContractContractObject()
                {
                    TransportGUID = Guid.NewGuid().ToString(),
                    BaseMService = new BaseServiceType()
                    {
                        Item = true
                    },
                    FIASHouseGuid = "cf9c25b6-c7a4-40db-81f2-c97bfa46b903",
                    StartDate = signingDate
                }],
                docNum = "11111111",
                signingDate = signingDate,
                effectiveDate = effectiveDate,
                planDateComptetion = planComptetionDate,
                contractBase = Registry58.Element8,
                dateDetailsType = new DateDetailsType()
                {
                    PeriodMetering = new DateDetailsTypePeriodMetering()
                    {
                        StartDate = new DeviceMeteringsDaySelectionType()
                        {
                            Item = (sbyte)20,
                            IsNextMonth = true
                        },
                        EndDate = new DeviceMeteringsDaySelectionType()
                        {
                            Item = (sbyte)25,
                            IsNextMonth = true
                        }
                    },
                    PaymentDocumentInterval = new DateDetailsTypePaymentDocumentInterval()
                    {
                        Item = true,
                        Item1 = true,
                        Item1ElementName = Item1ChoiceType4.NextMounth
                    },
                    PaymentInterval = new DateDetailsTypePaymentInterval()
                    {
                        Item = true,
                        Item1 = true,
                        Item1ElementName = Item1ChoiceType5.NextMounth
                    }
                },
                contractAttachment = [new AttachmentType()
                {
                    Name = "Наименование вложения",
                    Description = "Описание вложения",
                    Attachment = new Attachment()
                    {
                        AttachmentGUID = Guid.NewGuid().ToString()
                    },
                    AttachmentHASH = ""
                }]
            };
            var result = client.HouseManagement.ImportContractDataAsync(payload).Result;
            Console.WriteLine("Scenario execution " + (result != null ? "succeeded" : "failed"));
        }

        internal void ImportLivingHouseUOData()
        {
            var payload = new ImportLivingHouseUODataPayload()
            {
                fiasHouseGuid = Guid.Parse("db041c58-4a14-45ce-8ecc-d99727e9683f"),
                totalSquare = 29.3m,
                state = Registry24.Element2,
                lifeCycleStage = Registry338.Element1,
                usedYear = 2000,
                floorCount = 1,
                oktmo = new OKTMORefType()
                {
                    code = "98641101001"
                },
                olsonTZ = Registry32.Element11,
                culturalHeritage = false,
                isMunicipalProperty = false,
                isRegionProperty = false,
                conditionalNumber = "1234567890"
            };
            var result = client.HouseManagement.ImportHouseUODataAsync(payload).Result;
            Console.WriteLine("Scenario execution " + (result ? "succeeded" : "failed"));
        }

        internal void ImportMeteringDeviceData()
        {
            var result = client.HouseManagement.ImportMeteringDeviceDataAsync(new MeteringDeviceFullInformationType()
            {
                BasicChatacteristicts = new MeteringDeviceBasicCharacteristicsType()
                {
                    MeteringDeviceNumber = "Н43733003",
                    MeteringDeviceStamp = "-",
                    MeteringDeviceModel = "-",
                    CommissioningDate = new DateTime(2021, 1, 1),
                    CommissioningDateSpecified = true,
                    RemoteMeteringMode = false,
                    FirstVerificationDate = new DateTime(2021, 1, 1),
                    FirstVerificationDateSpecified = true,
                    VerificationInterval = new nsiRef()
                    {
                        Code = Registry16.Element4.Code,
                        GUID = Registry16.Element4.GUID
                    },
                    FactorySealDate = new DateTime(2021, 1, 1),
                    FactorySealDateSpecified = true,
                    TemperatureSensor = false,
                    PressureSensor = false,
                    Item = new MeteringDeviceBasicCharacteristicsTypeResidentialPremiseDevice()
                    {
                        PremiseGUID = ["6e011824-3d8a-48d7-b0ee-7b36be7096bf"],
                        AccountGUID = ["019917a8-6eb6-74cc-99b7-58350127ac50"]
                    }
                },
                Item = true,
                Items = [new MunicipalResourceNotElectricBaseType()
                {
                    MunicipalResource = new nsiRef()
                    {
                        Code = Registry2.Element5.Code,
                        GUID = Registry2.Element5.GUID
                    },
                    MeteringValue = "0"
                }]
            }).Result;
            Console.WriteLine("Scenario execution " + (result ? "succeeded" : "failed"));
        }

        internal void ImportNotificationData()
        {
            var payload = new ImportNotificationDataPayload()
            {
                topic = "Тема новости",
                isImportant = true,
                content = "Содержимое новости",
                destinations =
                [
                    new(ItemsChoiceType29.IsAll, true)
                ],
                isNotLimit = true,
                isShipOff = true
            };
            var result = client.HouseManagement.ImportNotificationDataAsync(payload).Result;
            Console.WriteLine("Scenario execution " + (result ? "succeeded" : "failed"));
        }

        internal void ImportSupplyResourceContractData()
        {
            var signingDate = new DateTime(2017, 01, 01);
            var effectiveDate = new DateTime(2017, 01, 01);
            var comptetionDate = new DateTime(2078, 12, 31);

            var objectAddressGuid = Guid.NewGuid().ToString();
            var contractSubjectGuid = Guid.NewGuid().ToString();

            var payload = new ImportSupplyResourceContractDataPayload()
            {
                isContract = false,
                contractNumber = "88888888",
                signingDate = signingDate,
                effectiveDate = effectiveDate,
                automaticRollOverOneYear = true,
                comptetionDate = comptetionDate,
                // TODO: Не дает выгрузить период
                //period = new SupplyResourceContractTypePeriod()
                //{
                //    Start = new SupplyResourceContractTypePeriodStart()
                //    {
                //        StartDate = 20
                //    },
                //    End = new SupplyResourceContractTypePeriodEnd()
                //    {
                //        EndDate = 25
                //    }
                //},
                counterparty = true,
                isPlannedVolume = false,
                contractSubject = [new SupplyResourceContractTypeContractSubject()
                {
                    TransportGUID = contractSubjectGuid,
                    ServiceType = new ContractSubjectTypeServiceType()
                    {
                        Code = Registry3.Element6.Code,
                        GUID = Registry3.Element6.GUID
                    },
                    MunicipalResource = new ContractSubjectTypeMunicipalResource()
                    {
                        Code = Registry239.Element4.Code,
                        GUID = Registry239.Element4.GUID
                    },
                    StartSupplyDate = effectiveDate,
                    EndSupplyDate = comptetionDate,
                    EndSupplyDateSpecified = true
                }],
                specifyingQualityIndicators = SupplyResourceContractTypeSpecifyingQualityIndicators.D,
                objectAddress = [new SupplyResourceContractTypeObjectAddress()
                {
                    TransportGUID = objectAddressGuid,
                    Pair = [new SupplyResourceContractTypeObjectAddressPair()
                    {
                        PairKey = contractSubjectGuid,
                        StartSupplyDate = effectiveDate,
                        EndSupplyDate = comptetionDate,
                        EndSupplyDateSpecified = true,
                        HeatingSystemType = new SupplyResourceContractTypeObjectAddressPairHeatingSystemType()
                        {
                            OpenOrNot = SupplyResourceContractTypeObjectAddressPairHeatingSystemTypeOpenOrNot.Opened,
                            CentralizedOrNot = SupplyResourceContractTypeObjectAddressPairHeatingSystemTypeCentralizedOrNot.Decentralized
                        }
                    }],
                    HouseType = ObjectAddressTypeHouseType.MKD,
                    HouseTypeSpecified = true,
                    FIASHouseGuid = "c48e5025-f77a-494f-8aab-f773da623a25",
                    ApartmentNumber = "1"
                }],
                quality = [new SupplyResourceContractTypeQuality()
                {
                    PairKey = contractSubjectGuid,
                    QualityIndicator = new nsiRef()
                    {
                        Code = Registry276.Element4.Code,
                        GUID = Registry276.Element4.GUID
                    },
                    IndicatorValue = new SupplyResourceContractTypeQualityIndicatorValue()
                    {
                        Items = [9.76m, 10m, "214"],
                        ItemsElementName = [ItemsChoiceType10.StartRange, ItemsChoiceType10.EndRange, ItemsChoiceType10.OKEI]
                    }
                }, new SupplyResourceContractTypeQuality()
                {
                    PairKey = contractSubjectGuid,
                    QualityIndicator = new nsiRef()
                    {
                        Code = Registry276.Element10.Code,
                        GUID = Registry276.Element10.GUID
                    },
                    IndicatorValue = new SupplyResourceContractTypeQualityIndicatorValue()
                    {
                        Items = [0.05m, 0.3m, "298"],
                        ItemsElementName = [ItemsChoiceType10.StartRange, ItemsChoiceType10.EndRange, ItemsChoiceType10.OKEI]
                    }
                }],
                billingDate = new SupplyResourceContractTypeBillingDate()
                {
                    Date = 1,
                    DateType = SupplyResourceContractTypeBillingDateDateType.N
                },
                paymentDate = new SupplyResourceContractTypePaymentDate()
                {
                    Date = 10,
                    DateType = SupplyResourceContractTypePaymentDateDateType.N
                },
                // TODO: Не дает задать этот параметр, из-за чего приходится удалять период
                //meteringDeviceInformation = true,
                volumeDepends = false,
                oneTimePayment = false,
                // TODO: Пока не понятно, откуда берется NormGUID
                //norm = [new SupplyResourceContractTypeNorm()
                //{
                //    PairKey = contractSubjectGuid,
                //    Items = [true],
                //    NormGUID = ""
                //}]
            };
            var result = client.HouseManagement.ImportSupplyResourceContractDataAsync(payload).Result;
            Console.WriteLine("Scenario execution " + (result != null ? "succeeded" : "failed"));
        }

        internal void ImportSupplyResourceContractProject()
        {
            var signingDate = new DateTime(2017, 01, 01);
            var effectiveDate = new DateTime(2017, 01, 01);
            var comptetionDate = new DateTime(2078, 12, 31);

            var contractSubjectGuid = Guid.NewGuid().ToString();

            var payload = new ImportSupplyResourceContractProjectPayload()
            {
                isContract = false,
                contractNumber = "88005553535",
                signingDate = signingDate,
                effectiveDate = effectiveDate,
                automaticRollOverOneYear = true,
                comptetionDate = comptetionDate,
                period = new SupplyResourceContractProjectTypePeriod()
                {
                    Start = new SupplyResourceContractProjectTypePeriodStart()
                    {
                        StartDate = 20
                    },
                    End = new SupplyResourceContractProjectTypePeriodEnd()
                    {
                        EndDate = 25
                    }
                },
                counterparty = true,
                isPlannedVolume = false,
                contractSubject = [new SupplyResourceContractProjectTypeContractSubject()
                {
                    TransportGUID = contractSubjectGuid,
                    ServiceType = new ContractSubjectTypeServiceType()
                    {
                        Code = Registry3.Element6.Code,
                        GUID = Registry3.Element6.GUID
                    },
                    MunicipalResource = new ContractSubjectTypeMunicipalResource()
                    {
                        Code = Registry239.Element4.Code,
                        GUID = Registry239.Element4.GUID
                    },
                    StartSupplyDate = effectiveDate,
                    EndSupplyDate = comptetionDate,
                    EndSupplyDateSpecified = true
                }],
                specifyingQualityIndicators = SupplyResourceContractProjectTypeSpecifyingQualityIndicators.D,
                quality = [new SupplyResourceContractProjectTypeQuality()
                {
                    PairKey = contractSubjectGuid,
                    QualityIndicator = new nsiRef()
                    {
                        Code = Registry276.Element4.Code,
                        GUID = Registry276.Element4.GUID
                    },
                    IndicatorValue = new SupplyResourceContractProjectTypeQualityIndicatorValue()
                    {
                        Items = [9.76m, 10m, "214"],
                        ItemsElementName = [ItemsChoiceType10.StartRange, ItemsChoiceType10.EndRange, ItemsChoiceType10.OKEI]
                    }
                }, new SupplyResourceContractProjectTypeQuality()
                {
                    PairKey = contractSubjectGuid,
                    QualityIndicator = new nsiRef()
                    {
                        Code = Registry276.Element10.Code,
                        GUID = Registry276.Element10.GUID
                    },
                    IndicatorValue = new SupplyResourceContractProjectTypeQualityIndicatorValue()
                    {
                        Items = [0.05m, 0.3m, "298"],
                        ItemsElementName = [ItemsChoiceType10.StartRange, ItemsChoiceType10.EndRange, ItemsChoiceType10.OKEI]
                    }
                }],
                billingDate = new SupplyResourceContractProjectTypeBillingDate()
                {
                    Date = 1,
                    DateType = SupplyResourceContractProjectTypeBillingDateDateType.N
                },
                paymentDate = new SupplyResourceContractProjectTypePaymentDate()
                {
                    Date = 10,
                    DateType = SupplyResourceContractProjectTypePaymentDateDateType.N
                },
                meteringDeviceInformation = true,
                volumeDepends = false,
                oneTimePayment = false,
                // TODO: Пока не понятно, откуда берется NormGUID
                //norm = [new SupplyResourceContractTypeNorm()
                //{
                //    PairKey = contractSubjectGuid,
                //    Items = [true],
                //    NormGUID = ""
                //}]
            };
            var result = client.HouseManagement.ImportSupplyResourceContractProjectAsync(payload).Result;
            Console.WriteLine("Scenario execution " + (result != null ? "succeeded" : "failed"));
        }
    }
}
