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; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows; 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 this._apelido; } set { this._apelido = value; base.OnPropertyChanged("Apelido"); } } public List Config { get; set; } public ObservableCollection ConfigFiltrados { get { return this._configFiltrados; } set { this._configFiltrados = value; base.OnPropertyChanged("ConfigFiltrados"); } } public ObservableCollection Contatos { get { return this._contatos; } set { this._contatos = value; base.OnPropertyChanged("Contatos"); } } public ObservableCollection Enderecos { get { return this._enderecos; } set { this._enderecos = value; base.OnPropertyChanged("Enderecos"); } } public bool IsExpanded { get { return this._isExpanded; } set { this._isExpanded = value; base.OnPropertyChanged("IsExpanded"); } } public string Pesquisa { get; set; } public Visibility PrefixoVisibility { get { return this._prefixo; } set { if (this.SelectedTelefone.get_TipoContato() != 2) { this._prefixo = Visibility.Visible; } else { this._prefixo = Visibility.Collapsed; } base.OnPropertyChanged("PrefixoVisibility"); } } public List Seguradoras { get; set; } public ObservableCollection SeguradorasFiltradas { get { return this._seguradorasFiltradas; } set { this._seguradorasFiltradas = value; this.IsExpanded = (value != null ? value.Count > 0 : false); base.OnPropertyChanged("SeguradorasFiltradas"); } } public SeguradoraEndereco SelectedEndereco { get { return this._selectedEndereco; } set { this._selectedEndereco = value; base.OnPropertyChanged("SelectedEndereco"); } } public Seguradora SelectedSeguradora { get { return this._selectedSeguradora; } set { long? nullable; this._selectedSeguradora = value; this.WorkOnSelectedSeguradora(value, true); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedSeguradora"); } } public SeguradoraContato SelectedTelefone { get { return this._selectedTelefone; } set { this._selectedTelefone = value; base.OnPropertyChanged("SelectedTelefone"); } } public Visibility TelefoneVisibility { get { return this._telefone; } set { if (this.SelectedTelefone.get_TipoContato() != 2) { this._telefone = Visibility.Visible; } else { this._telefone = Visibility.Collapsed; } base.OnPropertyChanged("TelefoneVisibility"); } } public Visibility TipoTelefoneVisibility { get { return this._tipoTelefone; } set { if (this.SelectedTelefone.get_TipoContato() != 2) { this._tipoTelefone = Visibility.Visible; } else { this._tipoTelefone = Visibility.Collapsed; } base.OnPropertyChanged("TipoTelefoneVisibility"); } } public decimal Tolerancia { get; set; } public SeguradoraViewModel() { this._servico = new SeguradoraServico(); this.Apelido = Recursos.Configuracoes.Any((ConfiguracaoSistema x) => x.get_Configuracao() == 6); base.EnableMenu = true; this.Seleciona(); } public void CancelarAlteracao() { if (this.CancelSeguradora != null && this.Seguradoras.Any((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id())) { DomainBase.Copy(this.Seguradoras.First((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()), this.CancelSeguradora); if (this.SeguradorasFiltradas.Count <= 0 || !this.SeguradorasFiltradas.Any((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id())) { this.SeguradorasFiltradas.Add(this.CancelSeguradora); } else { DomainBase.Copy(this.SeguradorasFiltradas.First((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()), this.CancelSeguradora); } this.SeguradorasFiltradas = new ObservableCollection(this.SeguradorasFiltradas); this.ConfigFiltrados = new ObservableCollection(this.ConfigFiltrados); this.SelectedSeguradora = this.SeguradorasFiltradas.First((Seguradora x) => x.get_Id() == this.CancelSeguradora.get_Id()); this.Enderecos = this.CancelEnderecos; this.Contatos = this.CancelContatos; } base.Alterar(false); } public void Clonar() { this.CancelSeguradora = (Seguradora)this.SelectedSeguradora.Clone(); this.CancelEnderecos = new ObservableCollection(this.Enderecos); this.CancelContatos = new ObservableCollection(this.Contatos); } public void ExcluirEndereco(SeguradoraEndereco endereco) { this.Enderecos.Remove(endereco); } public void ExcluirTelefone(SeguradoraContato contato) { this.Contatos.Remove(contato); } internal async Task> Filtrar(string value) { List seguradoras = await Task.Run>(() => this.FiltrarSeguradora(value)); return seguradoras; } internal async Task> FiltrarConfig(string value) { List configExtratoImports = await Task.Run>(() => this.FiltrarDescricao(value)); return configExtratoImports; } public List FiltrarDescricao(string filter) { this.ConfigFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Config) : new ObservableCollection( from x in this.Config where ValidationHelper.RemoveDiacritics(x.get_Descricao().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.get_Descricao() select x)); return this.ConfigFiltrados.ToList(); } public List FiltrarSeguradora(string filter) { this.Pesquisa = filter; this.SeguradorasFiltradas = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Seguradoras) : new ObservableCollection(this.Seguradoras.Where((Seguradora x) => { if (ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))) { return true; } return ValidationHelper.RemoveDiacritics(x.get_NomeSocial()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)); }).OrderByDescending((Seguradora x) => x.get_Ativo()).ThenBy((Seguradora x) => x.get_Nome()))); return this.SeguradorasFiltradas.ToList(); } public async void Incluir(Seguradora seguradora) { seguradora.set_Tolerancia(new decimal?(2)); seguradora.set_ToleranciaPremio(new decimal?(2)); seguradora.set_Ativo(true); await this._servico.Insert(seguradora); if (this._servico.Sucesso) { await this.SelecionaSeguradoras(seguradora); } } public void IncluirEndereco() { if (this.SelectedSeguradora == null) { return; } if (this.Enderecos == null) { this.Enderecos = new ObservableCollection(); } SeguradoraEndereco seguradoraEndereco = new SeguradoraEndereco(); seguradoraEndereco.set_Empresa(Recursos.Empresa); seguradoraEndereco.set_Seguradora(this.SelectedSeguradora); seguradoraEndereco.set_Tipo(4); SeguradoraEndereco seguradoraEndereco1 = seguradoraEndereco; this.Enderecos.Add(seguradoraEndereco1); this.SelectedEndereco = seguradoraEndereco1; } public void IncluirTelefone() { if (this.SelectedSeguradora == null) { return; } if (this.Contatos == null) { this.Contatos = new ObservableCollection(); } SeguradoraContato seguradoraContato = new SeguradoraContato(); seguradoraContato.set_Empresa(Recursos.Empresa); seguradoraContato.set_Seguradora(this.SelectedSeguradora); seguradoraContato.set_Tipo(new TipoTelefone?(2)); SeguradoraContato seguradoraContato1 = seguradoraContato; this.Contatos.Add(seguradoraContato1); this.SelectedTelefone = seguradoraContato1; } public async Task>> Salvar() { List> keyValuePairs; string str; List> keyValuePairs1 = this.SelectedSeguradora.Validate(); keyValuePairs1.AddRange(this.Validate(this.Contatos.ToList())); keyValuePairs1.AddRange(this.Validate(this.Enderecos.ToList())); if (keyValuePairs1.Count <= 0) { Seguradora selectedSeguradora = this.SelectedSeguradora; ObservableCollection contatos = this.Contatos; selectedSeguradora.set_Contatos(contatos.Where((SeguradoraContato x) => { if (!string.IsNullOrEmpty(x.get_NomeContato())) { return true; } return x.get_TipoContato() == 1; }).ToList()); Seguradora seguradora = this.SelectedSeguradora; ObservableCollection enderecos = this.Enderecos; seguradora.set_Enderecos(( from x in enderecos where !string.IsNullOrEmpty(x.get_Bairro()) select x).ToList()); Seguradora seguradora1 = await this._servico.Save(this.SelectedSeguradora, this.Config, false); Seguradora seguradora2 = seguradora1; str = (this.SelectedSeguradora.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); string str1 = str; if (this._servico.Sucesso) { base.RegistrarAcao(string.Concat(str1, " SEGURADORA \"", seguradora2.get_Nome(), "\""), seguradora2.get_Id(), new TipoTela?(13), string.Format("SEGURADORA \"{0}\", ID: {1}", seguradora2.get_Nome(), seguradora2.get_Id())); await this.SelecionaSeguradoras(null); this.FiltrarSeguradora(this.Pesquisa); SeguradoraViewModel seguradoraViewModel = this; Seguradora seguradora3 = this.SeguradorasFiltradas.FirstOrDefault((Seguradora x) => x.get_Id() == seguradora2.get_Id()); if (seguradora3 == null) { seguradora3 = this.SeguradorasFiltradas.FirstOrDefault(); } seguradoraViewModel.SelectedSeguradora = seguradora3; base.Alterar(false); base.ToggleSnackBar("SEGURADORA SALVA COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } return keyValuePairs; } private async void Seleciona() { base.Loading(true); await base.PermissaoTela(13); await this.SelecionaSeguradoras(null); base.Loading(false); } public async void SelecionaSeguradora(Seguradora seguradora) { base.Loading(true); this.SelectedSeguradora = seguradora; this.Contatos = await this._servico.BuscarContatos(this.SelectedSeguradora.get_Id()); this.Enderecos = await this._servico.BuscarEnderecos(this.SelectedSeguradora.get_Id()); this.Config = await this._servico.BuscarConfig(this.SelectedSeguradora.get_Id()); this.ConfigFiltrados = new ObservableCollection(this.Config); base.Loading(false); } private async Task SelecionaSeguradoras(Seguradora seguradora = null) { base.Loading(true); List seguradoras = await (new BaseServico()).BuscarSeguradorasAsync(); SeguradoraViewModel list = this; List seguradoras1 = seguradoras; IOrderedEnumerable ativo = from x in seguradoras1 orderby x.get_Ativo() descending select x; list.Seguradoras = ativo.ThenBy((Seguradora x) => x.get_Nome()).ToList(); this.SeguradorasFiltradas = new ObservableCollection(this.Seguradoras); if (seguradora != null) { this.SelecionaSeguradora(this.SeguradorasFiltradas.FirstOrDefault((Seguradora x) => x.get_Id() == seguradora.get_Id())); } else if (this.SeguradorasFiltradas.Count <= 0) { this.SelectedSeguradora = new Seguradora(); base.Alterar(false); base.EnableMenu = false; } else { this.SelecionaSeguradora(this.SeguradorasFiltradas.First()); } Recursos.Seguradoras = seguradoras; base.Loading(false); } public List> Validate(List endereco) { List> keyValuePairs = new List>(); if (endereco != null) { endereco.ForEach((SeguradoraEndereco x) => keyValuePairs.AddRange(x.Validate())); } return keyValuePairs.Distinct>().ToList>(); } public List> Validate(List contatos) { List list; List> keyValuePairs = new List>(); if (contatos != null) { contatos.ForEach((SeguradoraContato x) => keyValuePairs.AddRange(x.Validate())); } if (contatos != null) { list = ( from x in contatos where x.get_TipoContato() == 1 select x).ToList(); } else { list = null; } List seguradoraContatos = list; if (seguradoraContatos == null) { return keyValuePairs; } if (seguradoraContatos.Count > 4) { keyValuePairs.Add(new KeyValuePair("ASSISTÊNCIAS 24 HORAS", "NÃO É POSSÍVEL ADICIONAR MAIS QUE 4 NUMEROS DE ASSISTÊNCIA 24 HORAS")); } return keyValuePairs.Distinct>().ToList>(); } private void WorkOnSelectedSeguradora(Seguradora value, bool registrar = true) { decimal? nullable; bool contatos; bool enderecos; long? nullable1; decimal? tolerancia; this.CancelSeguradora = (value == null || value.get_Id() == 0 ? this.CancelSeguradora : (Seguradora)value.Clone()); this.CancelEnderecos = new ObservableCollection(this.Enderecos); this.CancelContatos = new ObservableCollection(this.Contatos); if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 13) { return; } Seguradora selectedSeguradora = this.SelectedSeguradora; if (selectedSeguradora != null) { contatos = selectedSeguradora.get_Contatos(); } else { contatos = false; } if (contatos) { this.Contatos = new ObservableCollection(this.SelectedSeguradora.get_Contatos()); } Seguradora seguradora = this.SelectedSeguradora; if (seguradora != null) { enderecos = seguradora.get_Enderecos(); } else { enderecos = false; } if (enderecos) { this.Enderecos = new ObservableCollection(this.SelectedSeguradora.get_Enderecos()); } if (registrar) { base.RegistrarAcao(string.Concat("ACESSOU SEGURADORA \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(13), string.Format("ID SEGURADORA: {0}", value.get_Id())); } this.LastAccessId = value.get_Id(); this.LastAccessTela = 13; Seguradora selectedSeguradora1 = this.SelectedSeguradora; if (selectedSeguradora1 != null) { nullable1 = new long?(selectedSeguradora1.get_Id()); } else { nullable1 = null; } long? nullable2 = nullable1; long id = value.get_Id(); if (nullable2.GetValueOrDefault() != id | !nullable2.HasValue) { this.SelectedSeguradora = this.SeguradorasFiltradas.FirstOrDefault((Seguradora x) => x.get_Id() == value.get_Id()); } Seguradora seguradora1 = this.SelectedSeguradora; if (seguradora1 != null) { tolerancia = seguradora1.get_Tolerancia(); } else { nullable = null; tolerancia = nullable; } nullable = tolerancia; this.Tolerancia = nullable.GetValueOrDefault(); } } }