using Gestor.Application.Actions; using Gestor.Application.Helpers; using Gestor.Application.Servicos; using Gestor.Application.Servicos.Generic; using Gestor.Application.ViewModels.Generic; using Gestor.Model.Domain.Configuracoes; using Gestor.Model.Domain.Ferramentas; 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 { public class TrilhaViewModel : BaseSegurosViewModel { private ObservableCollection _trilhas = new ObservableCollection(); private ObservableCollection _fases = new ObservableCollection(); private Trilha _selectedTrilha = new Trilha(); private Tarefa _selectedTarefa; private ObservableCollection _usuarios; private Usuario _selectedUsuario = new Usuario(); public ObservableCollection Fases { get { return this._fases; } set { this._fases = value; base.OnPropertyChanged("Fases"); } } public Tarefa SelectedTarefa { get { return this._selectedTarefa; } set { this._selectedTarefa = value; base.OnPropertyChanged("SelectedTarefa"); } } public Trilha SelectedTrilha { get { return this._selectedTrilha; } set { this._selectedTrilha = value; base.OnPropertyChanged("SelectedTrilha"); } } public Usuario SelectedUsuario { get { return this._selectedUsuario; } set { this._selectedUsuario = value; base.OnPropertyChanged("SelectedUsuario"); } } private TarefaServico Servico { get; } public ObservableCollection Trilhas { get { return this._trilhas; } set { this._trilhas = value; base.OnPropertyChanged("Trilhas"); } } public ObservableCollection Usuarios { get { return this._usuarios; } set { this._usuarios = value; base.OnPropertyChanged("Usuarios"); } } public TrilhaViewModel() { this.Servico = new TarefaServico(); List usuarios = new List(); Usuario usuario = new Usuario(); usuario.set_Id((long)0); usuario.set_Nome("TODOS OS USUÁRIOS"); usuarios.Add(usuario); List usuarios1 = usuarios; usuarios1.AddRange(Recursos.Usuarios.Where((Usuario x) => { if (Recursos.Usuario.get_IdEmpresa() != (long)1 && x.get_IdEmpresa() != Recursos.Usuario.get_IdEmpresa()) { return false; } return !x.get_Excluido(); }).OrderBy((Usuario x) => x.get_Nome()).ToList()); this.Usuarios = new ObservableCollection(usuarios1); this.SelectedUsuario = this.Usuarios.First(); Gestor.Application.Actions.Actions.AtualizaTrilhas = (Action)Delegate.Combine(Gestor.Application.Actions.Actions.AtualizaTrilhas, new Action(this.AtualizarTrilhas)); this.CarregaTrilhas(); } public void AtualizarTrilhas() { this.CarregaTrilhas(); } public async Task Cancelar() { await this.CarregarTrilha(this.SelectedTrilha); base.Alterar(false); } public async Task CarregarTrilha(Trilha trilha) { if (trilha != null && trilha.get_Id() != 0) { this.SelectedTrilha = trilha; List tarefas = await this.Servico.BuscarTarefas(trilha.get_Id()); List tarefas1 = tarefas; List fases = await this.Servico.BuscarFases(trilha.get_Id()); this.Fases = new ObservableCollection(); fases.ForEach((Fase x) => { x.set_Tarefas(tarefas1.Where((Tarefa t) => { if (this.SelectedUsuario != null && this.SelectedUsuario.get_Id() != 0 && this.SelectedUsuario.get_Id() != t.get_Usuario().get_Id() || t.get_Fase().get_Id() != x.get_Id()) { return false; } return t.get_Status() != 2; }).ToList()); this.Fases.Add(x); }); } } public async Task CarregarTrilhas(Trilha trilha = null) { Trilha trilha1; List trilhas = await this.Servico.BuscarTrilhas(); this.Trilhas = new ObservableCollection(trilhas); TrilhaViewModel trilhaViewModel = this; trilha1 = (trilha != null ? this.Trilhas.FirstOrDefault((Trilha x) => x.get_Id() == trilha.get_Id()) : trilhas.FirstOrDefault()); await trilhaViewModel.CarregarTrilha(trilha1); } public async void CarregaTrilhas() { await this.CarregarTrilhas(null); } public void Excluir(Fase origem, Tarefa data) { Tarefa tarefa = this.Fases.First((Fase x) => x.get_Id() == origem.get_Id()).get_Tarefas().First((Tarefa x) => x.get_Id() == data.get_Id()); this.Fases.First((Fase x) => x.get_Id() == origem.get_Id()).get_Tarefas().Remove(tarefa); } public async Task Excluir(Tarefa tarefa) { if (tarefa.get_Id() != 0) { string[] titulo = new string[] { "DESEJA REALMENTE EXCLUIR A TAREFA ", tarefa.get_Titulo(), "?", Environment.NewLine, "ESSE PROCEDIMENTO É IRREVERSÍVEL" }; if (await base.ShowMessage(string.Concat(titulo), "SIM", "NÃO", false)) { if (await this.Servico.Excluir(tarefa.get_Id())) { await this.CarregarTrilha(this.SelectedTrilha); base.Alterar(false); } else { base.Alterar(false); } } } } public void Inserir(Fase origem, Tarefa data, int index, bool aberto = false) { data.set_Aberto(aberto); data.set_Fase(origem); this.Fases.First((Fase x) => x.get_Id() == origem.get_Id()).get_Tarefas().Insert((index == -1 ? 0 : index), data); this.TrocarFase(data); } public async Task>> Salvar(Tarefa destino) { List> keyValuePairs; Tarefa tarefa = destino; List configuracoes = Recursos.Configuracoes; tarefa.set_AgendamentoRetroativo(configuracoes.Any((ConfiguracaoSistema x) => x.get_Configuracao() == 45)); List> keyValuePairs1 = destino.Validate(); if (keyValuePairs1.Count <= 0) { await this.Servico.Salvar(destino); if (this.Servico.Sucesso) { await this.CarregarTrilha(this.SelectedTrilha); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } return keyValuePairs; } public async Task Salvar(Trilha destino) { TrilhaViewModel.u003cu003ec__DisplayClass37_0 variable; this.SelectedTrilha = null; this.Fases = null; List fases = destino.get_Fases(); Trilha trilha = await this.Servico.Salvar(destino); if (this.Servico.Sucesso) { if (fases != null) { fases.ForEach((Fase x) => x.set_Trilha(trilha)); await this.Servico.Salvar(fases); if (!this.Servico.Sucesso) { variable = null; fases = null; return; } } await this.CarregarTrilhas(trilha); } variable = null; fases = null; } private async void TrocarFase(Tarefa tarefa) { await this.Servico.Salvar(tarefa); } public void Update() { this.Fases = new ObservableCollection(this.Fases); } } }