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.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.Ferramentas { public class RamoViewModel : BaseSegurosViewModel { private readonly RamoServico _servico; private Ramo _selectedRamo = new Ramo(); private decimal _iof; private ObservableCollection _ramosFiltrados = new ObservableCollection(); private bool _isExpanded; private List _coberturas = new List(); private ObservableCollection _coberturasFiltradas = new ObservableCollection(); public Ramo CancelRamo; public List Coberturas { get { return this._coberturas; } set { this._coberturas = value; base.OnPropertyChanged("Coberturas"); } } public ObservableCollection CoberturasFiltradas { get { return this._coberturasFiltradas; } set { this._coberturasFiltradas = value; base.OnPropertyChanged("CoberturasFiltradas"); } } public decimal Iof { get { return this._iof; } set { this._iof = value; base.OnPropertyChanged("Iof"); } } public bool IsExpanded { get { return this._isExpanded; } set { this._isExpanded = value; base.OnPropertyChanged("IsExpanded"); } } public List Ramos { get; set; } public ObservableCollection RamosFiltrados { get { return this._ramosFiltrados; } set { this._ramosFiltrados = value; this.IsExpanded = (value != null ? value.Count > 0 : false); base.OnPropertyChanged("RamosFiltrados"); } } public Ramo SelectedRamo { get { return this._selectedRamo; } set { long? nullable; this._selectedRamo = value; this.WorkOnSelectedRamo(value, true); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); base.OnPropertyChanged("SelectedRamo"); } } public RamoViewModel() { this._servico = new RamoServico(); base.EnableMenu = true; this.Seleciona(); } public void CancelarAlteracao() { if (this.CancelRamo != null && this.Ramos.Any((Ramo x) => x.get_Id() == this.CancelRamo.get_Id())) { DomainBase.Copy(this.Ramos.First((Ramo x) => x.get_Id() == this.CancelRamo.get_Id()), this.CancelRamo); if (this.RamosFiltrados.Count <= 0 || !this.RamosFiltrados.Any((Ramo x) => x.get_Id() == this.CancelRamo.get_Id())) { this.RamosFiltrados.Add(this.CancelRamo); } else { DomainBase.Copy(this.RamosFiltrados.First((Ramo x) => x.get_Id() == this.CancelRamo.get_Id()), this.CancelRamo); } this.RamosFiltrados = new ObservableCollection(this.RamosFiltrados); this.SelectedRamo = this.RamosFiltrados.First((Ramo x) => x.get_Id() == this.CancelRamo.get_Id()); } base.Alterar(false); } internal async Task> Filtrar(string value) { List ramos = await Task.Run>(() => this.FiltrarRamo(value)); return ramos; } internal async Task> FiltrarCobertura(string value) { List coberturaPadraos = await Task.Run>(() => this.FiltrarCoberturaRamo(value)); return coberturaPadraos; } public List FiltrarCoberturaRamo(string filter) { this.CoberturasFiltradas = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Coberturas) : new ObservableCollection( from x in this.Coberturas where ValidationHelper.RemoveDiacritics(x.get_Descricao().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) select x)); return this.CoberturasFiltradas.ToList(); } public List FiltrarRamo(string filter) { this.RamosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Ramos) : new ObservableCollection( from x in this.Ramos where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) orderby x.get_Ativo() descending, x.get_Nome() select x)); return this.RamosFiltrados.ToList(); } public async void Incluir(Ramo ramo) { ramo.set_Ativo(true); await this._servico.Insert(ramo); if (this._servico.Sucesso) { await this.SelecionaRamos(ramo); } } public async Task>> Salvar() { List> keyValuePairs; string str; string str1; this.SelectedRamo.set_Iof(this.Iof); List> keyValuePairs1 = this.SelectedRamo.Validate(); if (keyValuePairs1.Count <= 0) { Ramo selectedRamo = this.SelectedRamo; selectedRamo.set_Iof(selectedRamo.get_Iof() * new decimal(100)); str = (this.SelectedRamo.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str1 = str; Ramo ramo = await this._servico.Save(this.SelectedRamo, this.Coberturas); Ramo ramo1 = ramo; if (this._servico.Sucesso) { base.RegistrarAcao(string.Concat(str1, " RAMO \"", ramo1.get_Nome(), "\""), ramo1.get_Id(), new TipoTela?(12), string.Format("RAMO \"{0}\", ID: {1}", ramo1.get_Nome(), ramo1.get_Id())); if (!this.Ramos.Any((Ramo x) => x.get_Id() == ramo1.get_Id())) { this.Ramos.Add(ramo1); } else { DomainBase.Copy(this.Ramos.First((Ramo x) => x.get_Id() == ramo1.get_Id()), ramo1); } if (!this.RamosFiltrados.Any((Ramo x) => x.get_Id() == ramo1.get_Id())) { this.RamosFiltrados.Add(ramo1); } else { DomainBase.Copy(this.RamosFiltrados.First((Ramo x) => x.get_Id() == ramo1.get_Id()), ramo1); } this.RamosFiltrados = new ObservableCollection(this.RamosFiltrados); Recursos.Ramos = this.Ramos; await RamoViewModel.SelecionaCoberturas(); this.WorkOnSelectedRamo(ramo1, false); base.Alterar(false); base.ToggleSnackBar("RAMO SALVO COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } str1 = null; return keyValuePairs; } public async void Seleciona() { base.Loading(true); await base.PermissaoTela(12); await this.SelecionaRamos(null); base.Loading(false); } private static async Task SelecionaCoberturas() { Recursos.Coberturas = await (new BaseServico()).BuscaCoberturas(); } public void SelecionaRamo(Ramo ramo) { this.SelectedRamo = ramo; } private async Task SelecionaRamos(Ramo ramo = null) { Ramo ramo1; base.Loading(true); await RamoViewModel.SelecionaCoberturas(); List ramos = await (new BaseServico()).BuscarRamosAsync(); RamoViewModel list = this; List ramos1 = ramos; IOrderedEnumerable ativo = from x in ramos1 orderby x.get_Ativo() descending select x; list.Ramos = ativo.ThenBy((Ramo x) => x.get_Nome()).ToList(); this.RamosFiltrados = new ObservableCollection(this.Ramos); RamoViewModel ramoViewModel = this; ramo1 = (ramo == null ? this.RamosFiltrados.First() : this.RamosFiltrados.FirstOrDefault((Ramo x) => x.get_Id() == ramo.get_Id())); ramoViewModel.SelectedRamo = ramo1; Recursos.Ramos = ramos; base.Loading(false); } private void WorkOnSelectedRamo(Ramo value, bool registrar = true) { long? nullable; this.CancelRamo = (value == null || value.get_Id() == 0 ? this.CancelRamo : (Ramo)value.Clone()); if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 12) { return; } this.Coberturas = ( from x in Recursos.Coberturas where x.get_IdRamo() == value.get_Id() orderby x.get_Padrao() descending, x.get_Descricao() select x).ToList(); this.CoberturasFiltradas = new ObservableCollection(this.Coberturas); this.Iof = value.get_Iof() * new decimal(1, 0, 0, false, 2); if (registrar) { base.RegistrarAcao(string.Concat("ACESSOU RAMO \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(12), string.Format("ID RAMO: {0}", value.get_Id())); } this.LastAccessId = value.get_Id(); this.LastAccessTela = 12; Ramo selectedRamo = this.SelectedRamo; if (selectedRamo != null) { nullable = new long?(selectedRamo.get_Id()); } else { nullable = null; } long? nullable1 = nullable; long id = value.get_Id(); if (nullable1.GetValueOrDefault() != id | !nullable1.HasValue) { this.SelectedRamo = this.RamosFiltrados.FirstOrDefault((Ramo x) => x.get_Id() == value.get_Id()); } } } }