using Gestor.Application.Servicos.Financeiro; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Domain.Financeiro; using Gestor.Model.Domain.Generic; using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace Gestor.Application.ViewModels.Financeiro { public class PlanoViewModel : BaseFinanceiroViewModel { private readonly PlanoServico _planoServico; private Plano _selectedPlano; public Plano Cancel; public Plano SelectedPlano { get { return this._selectedPlano; } set { long? nullable; this._selectedPlano = value; if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedPlano"); } } public PlanoViewModel(Plano plano) { this._planoServico = new PlanoServico(); this.Seleciona(plano); } public void Cancelar() { base.Loading(true); if (this.SelectedPlano.get_Id() != 0) { base.Alterar(false); base.Loading(false); return; } this.SelectedPlano = new Plano(); base.Alterar(false); base.Loading(false); } public void Incluir() { Plano plano = new Plano(); plano.set_Ativo(true); this.SelectedPlano = plano; base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; List> keyValuePairs1 = this.SelectedPlano.Validate(); List> keyValuePairs2 = keyValuePairs1; keyValuePairs2.AddRange(await this.Validate()); keyValuePairs2 = null; if (keyValuePairs1.Count <= 0) { this.SelectedPlano = await this._planoServico.Save(this.SelectedPlano); if (this._planoServico.Sucesso) { base.Alterar(false); base.ToggleSnackBar("PLANO SALVO COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } keyValuePairs1 = null; return keyValuePairs; } private async void Seleciona(Plano plano) { base.Loading(true); await base.PermissaoTela(27); if (plano != null) { this.SelectedPlano = plano; } else { Plano plano1 = new Plano(); plano1.set_Descricao(""); plano1.set_Ativo(true); this.SelectedPlano = plano1; } base.Loading(false); } public async Task>> Validate() { List> keyValuePairs = new List>(); bool flag = true; List planos = await (new BaseServico()).BuscarPlanoAsync(); if (planos.Count > 0) { planos.ForEach((Plano x) => { if (x.get_Id() == this.SelectedPlano.get_Id()) { return; } if (x.get_Descricao() == this.SelectedPlano.get_Descricao()) { flag = false; } }); } if (!flag) { keyValuePairs.Add(new KeyValuePair("Descricao", "UM PLANO COM ESSE NOME JÁ EXISTE.")); } List> keyValuePairs1 = keyValuePairs; keyValuePairs = null; return keyValuePairs1; } } }