using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using Gestor.Application.Helpers; using Gestor.Application.Servicos.Ferramentas; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Common.Validation; using Gestor.Model.Common; using Gestor.Model.Domain.Configuracoes; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; namespace Gestor.Application.ViewModels.Ferramentas; public class SeguradoraViewModel : BaseSegurosViewModel { private readonly SeguradoraServico _servico; private bool _apelido; private Seguradora _selectedSeguradora; private SeguradoraEndereco _selectedEndereco = new SeguradoraEndereco(); private ObservableCollection _contatos = new ObservableCollection(); private ObservableCollection _enderecos = new ObservableCollection(); private SeguradoraContato _selectedTelefone = new SeguradoraContato(); private Visibility _tipoTelefone; private Visibility _telefone; private Visibility _prefixo; private ObservableCollection _configFiltrados = new ObservableCollection(); public Seguradora CancelSeguradora; public ObservableCollection CancelEnderecos; public ObservableCollection CancelContatos; private ObservableCollection _seguradorasFiltradas = new ObservableCollection(); private bool _isExpanded; public bool Apelido { get { return _apelido; } set { _apelido = value; OnPropertyChanged("Apelido"); } } public Seguradora SelectedSeguradora { get { return _selectedSeguradora; } set { _selectedSeguradora = value; WorkOnSelectedSeguradora(value); VerificarEnables((value != null) ? new long?(((DomainBase)value).Id) : null); OnPropertyChanged("SelectedSeguradora"); } } public SeguradoraEndereco SelectedEndereco { get { return _selectedEndereco; } set { _selectedEndereco = value; OnPropertyChanged("SelectedEndereco"); } } public ObservableCollection Contatos { get { return _contatos; } set { _contatos = value; OnPropertyChanged("Contatos"); } } public ObservableCollection Enderecos { get { return _enderecos; } set { _enderecos = value; OnPropertyChanged("Enderecos"); } } public SeguradoraContato SelectedTelefone { get { return _selectedTelefone; } set { _selectedTelefone = value; OnPropertyChanged("SelectedTelefone"); } } public Visibility TipoTelefoneVisibility { get { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return _tipoTelefone; } set { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Invalid comparison between Unknown and I4 //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Unknown result type (might be due to invalid IL or missing references) if ((int)SelectedTelefone.TipoContato == 2) { _tipoTelefone = (Visibility)2; } else { _tipoTelefone = (Visibility)0; } OnPropertyChanged("TipoTelefoneVisibility"); } } public Visibility TelefoneVisibility { get { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return _telefone; } set { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Invalid comparison between Unknown and I4 //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Unknown result type (might be due to invalid IL or missing references) if ((int)SelectedTelefone.TipoContato == 2) { _telefone = (Visibility)2; } else { _telefone = (Visibility)0; } OnPropertyChanged("TelefoneVisibility"); } } public Visibility PrefixoVisibility { get { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return _prefixo; } set { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Invalid comparison between Unknown and I4 //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Unknown result type (might be due to invalid IL or missing references) if ((int)SelectedTelefone.TipoContato == 2) { _prefixo = (Visibility)2; } else { _prefixo = (Visibility)0; } OnPropertyChanged("PrefixoVisibility"); } } public ObservableCollection ConfigFiltrados { get { return _configFiltrados; } set { _configFiltrados = value; OnPropertyChanged("ConfigFiltrados"); } } public string Pesquisa { get; set; } public decimal Tolerancia { get; set; } public ObservableCollection SeguradorasFiltradas { get { return _seguradorasFiltradas; } set { _seguradorasFiltradas = value; IsExpanded = value != null && value.Count > 0; OnPropertyChanged("SeguradorasFiltradas"); } } public bool IsExpanded { get { return _isExpanded; } set { _isExpanded = value; OnPropertyChanged("IsExpanded"); } } public List Seguradoras { get; set; } public List Config { get; set; } public SeguradoraViewModel() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown _servico = new SeguradoraServico(); Apelido = Recursos.Configuracoes.Any((ConfiguracaoSistema x) => (int)x.Configuracao == 6); base.EnableMenu = true; Seleciona(); } private async void Seleciona() { Loading(isLoading: true); await PermissaoTela((TipoTela)13); await SelecionaSeguradoras(); Loading(isLoading: false); } private async Task SelecionaSeguradoras(Seguradora seguradora = null) { Loading(isLoading: true); List list = await new BaseServico().BuscarSeguradorasAsync(); Seguradoras = (from x in list orderby x.Ativo descending, x.Nome select x).ToList(); SeguradorasFiltradas = new ObservableCollection(Seguradoras); if (seguradora != null) { SelecionaSeguradora(((IEnumerable)SeguradorasFiltradas).FirstOrDefault((Func)((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)seguradora).Id))); } else if (SeguradorasFiltradas.Count > 0) { SelecionaSeguradora(SeguradorasFiltradas.First()); } else { SelectedSeguradora = new Seguradora(); Alterar(alterar: false); base.EnableMenu = false; } Recursos.Seguradoras = list; Loading(isLoading: false); } public List> Validate(List endereco) { List> errors = new List>(); endereco?.ForEach(delegate(SeguradoraEndereco x) { errors.AddRange(x.Validate()); }); return errors.Distinct().ToList(); } public List> Validate(List contatos) { List> errors = new List>(); contatos?.ForEach(delegate(SeguradoraContato x) { errors.AddRange(x.Validate()); }); List list = contatos?.Where((SeguradoraContato x) => (int)x.TipoContato == 1).ToList(); if (list == null) { return errors; } if (list.Count > 4) { errors.Add(new KeyValuePair("ASSISTÊNCIAS 24 HORAS", "NÃO É POSSÍVEL ADICIONAR MAIS QUE 4 NUMEROS DE ASSISTÊNCIA 24 HORAS")); } return errors.Distinct().ToList(); } public void IncluirTelefone() { //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_0045: Expected O, but got Unknown if (SelectedSeguradora != null) { if (Contatos == null) { Contatos = new ObservableCollection(); } SeguradoraContato val = new SeguradoraContato { Empresa = Recursos.Empresa, Seguradora = SelectedSeguradora, Tipo = (TipoTelefone)2 }; Contatos.Add(val); SelectedTelefone = val; } } public void ExcluirTelefone(SeguradoraContato contato) { Contatos.Remove(contato); } public void IncluirEndereco() { //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Expected O, but got Unknown if (SelectedSeguradora != null) { if (Enderecos == null) { Enderecos = new ObservableCollection(); } SeguradoraEndereco val = new SeguradoraEndereco { Empresa = Recursos.Empresa, Seguradora = SelectedSeguradora, Tipo = (TipoEndereco)4 }; Enderecos.Add(val); SelectedEndereco = val; } } public void ExcluirEndereco(SeguradoraEndereco endereco) { Enderecos.Remove(endereco); } public async Task>> Salvar() { List> list = SelectedSeguradora.Validate(); list.AddRange(Validate(Contatos.ToList())); list.AddRange(Validate(Enderecos.ToList())); if (list.Count > 0) { return list; } SelectedSeguradora.Contatos = Contatos.Where((SeguradoraContato x) => !string.IsNullOrEmpty(x.NomeContato) || (int)x.TipoContato == 1).ToList(); SelectedSeguradora.Enderecos = Enderecos.Where((SeguradoraEndereco x) => !string.IsNullOrEmpty(((EnderecoBase)x).Bairro)).ToList(); Seguradora value = await _servico.Save(SelectedSeguradora, Config); string text = ((((DomainBase)SelectedSeguradora).Id == 0L) ? "INCLUIU" : "ALTEROU"); if (!_servico.Sucesso) { return null; } RegistrarAcao(text + " SEGURADORA \"" + value.Nome + "\"", ((DomainBase)value).Id, (TipoTela)13, $"SEGURADORA \"{value.Nome}\", ID: {((DomainBase)value).Id}"); await SelecionaSeguradoras(); FiltrarSeguradora(Pesquisa); SelectedSeguradora = ((IEnumerable)SeguradorasFiltradas).FirstOrDefault((Func)((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)value).Id)) ?? SeguradorasFiltradas.FirstOrDefault(); Alterar(alterar: false); ToggleSnackBar("SEGURADORA SALVA COM SUCESSO"); return null; } private void WorkOnSelectedSeguradora(Seguradora value, bool registrar = true) { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_008b: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Invalid comparison between Unknown and I4 //IL_014c: Unknown result type (might be due to invalid IL or missing references) CancelSeguradora = (Seguradora)((value == null || ((DomainBase)value).Id == 0L) ? ((object)CancelSeguradora) : ((object)(Seguradora)((DomainBase)value).Clone())); CancelEnderecos = new ObservableCollection(Enderecos); CancelContatos = new ObservableCollection(Contatos); if (value == null || ((DomainBase)value).Id == 0L || (LastAccessId == ((DomainBase)value).Id && (int)LastAccessTela == 13)) { return; } Seguradora selectedSeguradora = SelectedSeguradora; if (((selectedSeguradora != null) ? selectedSeguradora.Contatos : null) != null) { Contatos = new ObservableCollection(SelectedSeguradora.Contatos); } Seguradora selectedSeguradora2 = SelectedSeguradora; if (((selectedSeguradora2 != null) ? selectedSeguradora2.Enderecos : null) != null) { Enderecos = new ObservableCollection(SelectedSeguradora.Enderecos); } if (registrar) { RegistrarAcao("ACESSOU SEGURADORA \"" + value.Nome + "\"", ((DomainBase)value).Id, (TipoTela)13, $"ID SEGURADORA: {((DomainBase)value).Id}"); } LastAccessId = ((DomainBase)value).Id; LastAccessTela = (TipoTela)13; Seguradora selectedSeguradora3 = SelectedSeguradora; if (((selectedSeguradora3 != null) ? new long?(((DomainBase)selectedSeguradora3).Id) : null) != ((DomainBase)value).Id) { SelectedSeguradora = ((IEnumerable)SeguradorasFiltradas).FirstOrDefault((Func)((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)value).Id)); } Seguradora selectedSeguradora4 = SelectedSeguradora; Tolerancia = ((selectedSeguradora4 != null) ? selectedSeguradora4.Tolerancia : null).GetValueOrDefault(); } public void CancelarAlteracao() { if (CancelSeguradora != null && Seguradoras.Any((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelSeguradora).Id)) { DomainBase.Copy(Seguradoras.First((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelSeguradora).Id), CancelSeguradora); if (SeguradorasFiltradas.Count > 0 && SeguradorasFiltradas.Any((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelSeguradora).Id)) { DomainBase.Copy(SeguradorasFiltradas.First((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelSeguradora).Id), CancelSeguradora); } else { SeguradorasFiltradas.Add(CancelSeguradora); } SeguradorasFiltradas = new ObservableCollection(SeguradorasFiltradas); ConfigFiltrados = new ObservableCollection(ConfigFiltrados); SelectedSeguradora = SeguradorasFiltradas.First((Seguradora x) => ((DomainBase)x).Id == ((DomainBase)CancelSeguradora).Id); Enderecos = CancelEnderecos; Contatos = CancelContatos; } Alterar(alterar: false); } internal async Task> Filtrar(string value) { return await Task.Run(() => FiltrarSeguradora(value)); } public List FiltrarSeguradora(string filter) { Pesquisa = filter; SeguradorasFiltradas = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(Seguradoras) : new ObservableCollection(from x in Seguradoras where ValidationHelper.RemoveDiacritics(x.Nome.Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) || ValidationHelper.RemoveDiacritics(x.NomeSocial).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.Ativo descending, x.Nome select x)); return SeguradorasFiltradas.ToList(); } internal async Task> FiltrarConfig(string value) { return await Task.Run(() => FiltrarDescricao(value)); } public List FiltrarDescricao(string filter) { ConfigFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(Config) : new ObservableCollection(from x in Config where ValidationHelper.RemoveDiacritics(x.Descricao.Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.Descricao select x)); return ConfigFiltrados.ToList(); } public async void SelecionaSeguradora(Seguradora seguradora) { Loading(isLoading: true); SelectedSeguradora = seguradora; Contatos = await _servico.BuscarContatos(((DomainBase)SelectedSeguradora).Id); Enderecos = await _servico.BuscarEnderecos(((DomainBase)SelectedSeguradora).Id); Config = await _servico.BuscarConfig(((DomainBase)SelectedSeguradora).Id); ConfigFiltrados = new ObservableCollection(Config); Loading(isLoading: false); } public void Clonar() { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Expected O, but got Unknown CancelSeguradora = (Seguradora)((DomainBase)SelectedSeguradora).Clone(); CancelEnderecos = new ObservableCollection(Enderecos); CancelContatos = new ObservableCollection(Contatos); } public async void Incluir(Seguradora seguradora) { seguradora.Tolerancia = 2; seguradora.ToleranciaPremio = 2; seguradora.Ativo = true; await _servico.Insert(seguradora); if (_servico.Sucesso) { await SelecionaSeguradoras(seguradora); } } }