using Gestor.Application.Servicos.Generic; using Gestor.Application.Servicos.Seguros; using Gestor.Application.ViewModels.Generic; 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.Drawer { public class ExpedicaoViewModel : BaseSegurosViewModel { private readonly ExpedicaoServico _servico; private readonly Documento _documento; private bool _carregando; private Expedicao _selectedExpedicao = new Expedicao(); private ObservableCollection _expedicoes = new ObservableCollection(); public bool Carregando { get { return this._carregando; } set { this._carregando = value; base.EnableMenu = !value; base.OnPropertyChanged("Carregando"); } } public ObservableCollection Expedicoes { get { return this._expedicoes; } set { this._expedicoes = value; base.OnPropertyChanged("Expedicoes"); } } public Expedicao SelectedExpedicao { get { return this._selectedExpedicao; } set { long? nullable; this._selectedExpedicao = value; if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedExpedicao"); } } public ExpedicaoViewModel(Documento documento) { this._servico = new ExpedicaoServico(); this._documento = documento; this.Seleciona(documento); } public async void CancelarAlteracao() { Expedicao expedicao; this.Carregando = true; long id = this.SelectedExpedicao.get_Id(); await this.Carregar(this.SelectedExpedicao.get_Apolice()); ExpedicaoViewModel expedicaoViewModel = this; if (this.Expedicoes.Count <= 0 || id <= (long)0) { expedicao = (this.Expedicoes.Count <= 0 || id != 0 ? new Expedicao() : this.Expedicoes.First()); } else { expedicao = this.Expedicoes.FirstOrDefault((Expedicao x) => x.get_Id() == id); } expedicaoViewModel.SelectedExpedicao = expedicao; base.Alterar(false); this.Carregando = false; } private async Task Carregar(Documento documento) { this.Expedicoes = new ObservableCollection(await this._servico.BuscarExpedicoes(documento)); } public async Task Excluir() { bool flag; Expedicao expedicao; if (this.SelectedExpedicao == null || this.SelectedExpedicao.get_Id() == 0) { flag = false; } else if (await base.ShowMessage("DESEJA EXCLUIR?", "SIM", "NÃO", false)) { this.Carregando = true; if (await this._servico.Delete(this.SelectedExpedicao)) { int num = this.Expedicoes.IndexOf(this.SelectedExpedicao); this.Expedicoes.Remove(this.SelectedExpedicao); if (this.Expedicoes.Count <= 0) { this.SelectedExpedicao = new Expedicao(); } else { ExpedicaoViewModel expedicaoViewModel = this; expedicao = (num < this.Expedicoes.Count ? this.Expedicoes.ElementAt(num) : this.Expedicoes.Last()); expedicaoViewModel.SelectedExpedicao = expedicao; } this.Carregando = false; flag = true; } else { this.Carregando = false; flag = false; } } else { flag = false; } return flag; } public void Incluir() { Expedicao expedicao = new Expedicao(); expedicao.set_Apolice(this._documento); this.SelectedExpedicao = expedicao; base.Alterar(true); } public async Task>> Salvar() { List> keyValuePairs; this.SelectedExpedicao = await this._servico.Save(this.SelectedExpedicao); if (this._servico.Sucesso) { long id = this.SelectedExpedicao.get_Id(); await this.Carregar(this.SelectedExpedicao.get_Apolice()); this.SelectedExpedicao = this.Expedicoes.FirstOrDefault((Expedicao x) => x.get_Id() == id); base.Alterar(false); keyValuePairs = null; } else { keyValuePairs = null; } return keyValuePairs; } private async void Seleciona(Documento documento) { this.Carregando = true; await base.PermissaoTela(46); await this.Carregar(documento); if (this.Expedicoes.Count <= 0) { this.SelectedExpedicao = new Expedicao(); base.Alterar(false); base.EnableMenu = false; } else { this.SelectedExpedicao = this.Expedicoes.First(); } this.Carregando = false; } } }