protected override async Task <EditBtcPayServerDataViewModel> BuildViewModel(ExternalServiceData data)
        {
            var client     = new BtcPayServerService(data);
            var clientData = client.GetData();

            return(new EditBtcPayServerDataViewModel()
            {
                Seed = clientData.Seed ?? new Mnemonic(Wordlist.English, WordCount.Twelve).ToString(),
                Server = clientData.Server,
                PairingUrl = await client.GetPairingUrl(data.Name),
                Paired = await client.CheckAccess()
            });
        }
        public async Task <IActionResult> EditData(string identifier)
        {
            var result = await GetExternalServiceData(identifier);

            if (result.Error != null)
            {
                return(result.Error);
            }

            var client = new BtcPayServerService(result.Data);

            var clientData = client.GetData();

            return(View(new EditBtcPayServerDataViewModel()
            {
                Seed = clientData.Seed ?? new Mnemonic(Wordlist.English, WordCount.Twelve).ToString(),
                Server = clientData.Server,
                PairingUrl = await client.GetPairingUrl(),
                Paired = await client.CheckAccess()
            }));
        }
        BuildModel(EditBtcPayServerDataViewModel viewModel, ExternalServiceData mainModel)
        {
            if (viewModel.Action == "unpair")
            {
                viewModel.Seed = null;
            }

            //current External Service data
            var oldData = mainModel.Get <BtcPayServerExternalServiceData>();


            if (oldData.Seed == viewModel.Seed && oldData.Server == viewModel.Server)
            {
                viewModel.LastCheck = oldData.LastCheck;
                viewModel.MonitoredInvoiceStatuses = oldData.MonitoredInvoiceStatuses;
                viewModel.PairedDate = oldData.PairedDate;
            }
            else
            {
                viewModel.PairedDate = DateTime.Now;
            }

            mainModel.Set((BtcPayServerExternalServiceData)viewModel);
            var service = new BtcPayServerService(mainModel);

            if (!ModelState.IsValid)
            {
                var serviceData = service.GetData();
                return(null, new EditBtcPayServerDataViewModel()
                {
                    Seed = serviceData.Seed ?? new Mnemonic(Wordlist.English, WordCount.Twelve).ToString(),
                    Server = serviceData.Server,
                    PairingUrl = await service.GetPairingUrl(mainModel.Name),
                    Paired = await service.CheckAccess()
                });
            }


            if (!await service.CheckAccess())
            {
                viewModel.Seed = viewModel.Seed ?? new Mnemonic(Wordlist.English, WordCount.Twelve).ToString();
                service.SetData(viewModel);
                viewModel.PairingUrl = await service.GetPairingUrl(mainModel.Name);

                viewModel.Paired = false;
                if (!string.IsNullOrEmpty(viewModel.PairingCode))
                {
                    var client = service.ConstructClient();
                    await client.AuthorizeClient(new PairingCode(viewModel.PairingCode));

                    if (!await service.CheckAccess())
                    {
                        ModelState.AddModelError(string.Empty, "Could not pair with pairing code");
                        return(null, viewModel);
                    }
                }

                ModelState.AddModelError(string.Empty, "Cannot proceed until paired");
                return(null, viewModel);
            }

            return(mainModel, null);
        }
        public async Task <IActionResult> EditData(string identifier, EditBtcPayServerDataViewModel data, string action)
        {
            var result = await GetExternalServiceData(identifier);

            if (result.Error != null)
            {
                return(result.Error);
            }

            if (action == "unpair")
            {
                data.Seed = null;
            }
            //current External Service data
            var externalServiceData = result.Data;

            //current External Service data
            var oldData = externalServiceData.Get <BtcPayServerExternalServiceData>();


            if (oldData.Seed == data.Seed && oldData.Server == data.Server)
            {
                data.LastCheck = oldData.LastCheck;
                data.MonitoredInvoiceStatuses = oldData.MonitoredInvoiceStatuses;
                data.PairedDate = oldData.PairedDate;
            }
            else
            {
                data.PairedDate = DateTime.Now;
            }

            externalServiceData.Set((BtcPayServerExternalServiceData)data);
            var service = new BtcPayServerService(externalServiceData);

            if (!ModelState.IsValid)
            {
                var serviceData = service.GetData();
                return(View(new EditBtcPayServerDataViewModel()
                {
                    Seed = serviceData.Seed ?? new Mnemonic(Wordlist.English, WordCount.Twelve).ToString(),
                    Server = serviceData.Server,
                    PairingUrl = await service.GetPairingUrl(),
                    Paired = await service.CheckAccess()
                }));
            }


            if (!await service.CheckAccess())
            {
                data.Seed = data.Seed ?? new Mnemonic(Wordlist.English, WordCount.Twelve).ToString();
                service.SetData(data);
                data.PairingUrl = await service.GetPairingUrl();

                data.Paired = false;
                if (!string.IsNullOrEmpty(data.PairingCode))
                {
                    var client = service.ConstructClient();
                    await client.AuthorizeClient(new PairingCode(data.PairingCode));

                    if (!await service.CheckAccess())
                    {
                        ModelState.AddModelError(string.Empty, "Could not pair with pairing code");
                        return(View(data));
                    }
                }

                ModelState.AddModelError(string.Empty, "Cannot proceed until paired");

                return(View(data));
            }

            await _externalServiceManager.AddOrUpdateExternalServiceData(externalServiceData);

            return(RedirectToAction("EditExternalService", "ExternalServices", new
            {
                id = externalServiceData.Id,
                statusMessage = "Btcpayserver Data updated"
            }));
        }