using Gestor.Application.Servicos.Generic; using Gestor.Application.Servicos.Seguros; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace Gestor.Application.ViewModels.Seguros { public class PerfilViewModel : BaseViewModel { private readonly PerfilServico _servico; private readonly Controle _controle; public long CancelPerfil; private Perfil _selectedPerfil = new Perfil(); private ObservableCollection _perfis; public ObservableCollection Perfis { get { return this._perfis; } set { this._perfis = value; base.OnPropertyChanged("Perfis"); } } public Perfil SelectedPerfil { get { return this._selectedPerfil; } set { long? nullable; this._selectedPerfil = value; if (value != null && value.get_Id() > (long)0) { this.CancelPerfil = value.get_Id(); } if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedPerfil"); } } public PerfilViewModel(Controle controle) { this._servico = new PerfilServico(); this._controle = controle; this.Seleciona(controle.get_Id()); } public void CancelarAlteracao() { base.Loading(true); if (this.CancelPerfil > (long)0) { this.SelectedPerfil = this.Perfis.FirstOrDefault((Perfil x) => x.get_Id() == this.CancelPerfil); } base.Alterar(false); base.Loading(false); } public async Task Excluir() { if (this.SelectedPerfil != null && this.SelectedPerfil.get_Id() != 0) { if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false)) { base.Loading(true); bool selectedPerfil = (object)this.SelectedPerfil == (object)this.Perfis.Last(); if (await this._servico.Delete(this.SelectedPerfil)) { if (selectedPerfil) { int num = this.Perfis.IndexOf(this.SelectedPerfil); if (num <= 0) { await this.SelecionaPerfis(this._controle.get_Id(), (long)0); } else { await this.SelecionaPerfis(this._controle.get_Id(), this.Perfis[num - 1].get_Id()); } } else if (this.Perfis.Count > 0) { await this.SelecionaPerfis(this._controle.get_Id(), this.Perfis[this.Perfis.IndexOf(this.SelectedPerfil) + 1].get_Id()); } base.Loading(false); base.ToggleSnackBar("PERFIL EXCLUÍDO COM SUCESSO", true); } else { base.Loading(false); } } } } public void Incluir() { Perfil perfil = new Perfil(); perfil.set_Controle(this._controle); perfil.set_Cliente(this._controle.get_Cliente()); perfil.set_UsoProfissional(new bool?(false)); perfil.set_AntiFurto(new Antifurto?(0)); perfil.set_Isencao(new bool?(false)); perfil.set_SeguroVida(new bool?(false)); perfil.set_EstenderCobertura(new bool?(false)); this.SelectedPerfil = perfil; base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; List> keyValuePairs1 = this.SelectedPerfil.Validate(); if (keyValuePairs1.Count <= 0) { this.SelectedPerfil = await this._servico.Save(this.SelectedPerfil); if (this._servico.Sucesso) { await this.SelecionaPerfis(this._controle.get_Id(), this.SelectedPerfil.get_Id()); base.ToggleSnackBar("PERFIL SALVO COM SUCESSO", true); base.Alterar(false); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } return keyValuePairs; } private async void Seleciona(long id) { base.Loading(true); await base.PermissaoTela(32); await this.SelecionaPerfis(id, (long)0); base.Loading(false); } private async Task SelecionaPerfis(long id, long perfilId = 0L) { Perfil perfil; base.Loading(true); this.Perfis = new ObservableCollection(await this._servico.BuscarPerfis(id)); PerfilViewModel perfilViewModel = this; perfil = (perfilId == 0 ? this.Perfis.FirstOrDefault() : this.Perfis.First((Perfil x) => x.get_Id() == perfilId)); perfilViewModel.SelectedPerfil = perfil; base.Loading(false); } } }