using SewerStammGen.Shared.Domain;
using SewerStammGen.WPF.Interface.Navigator;
using SewerStammGen.WPF.ViewModel.State;
using Shared.Contracts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SewerStammGen.WPF.ViewModel
{
    internal class ProjektEditViewModel : BaseViewModel
    {
        private Projekt _model;
        private int ProjektID;
        private readonly IDataService<Projekt> _dataService;
        private readonly IRenavigator _renavigator;

        public ICommand Speichern { get; set; }
        public string ProjektName
        {
            get => _model.Projektname;
            set
            {
                if(_model.Projektname != value)
                {
                    _model.Projektname = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Erstelldatum 
        {
            get => _model.Erstelldatum;
            set
            {
                if (_model.Erstelldatum != value)
                {
                    _model.Erstelldatum = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Strasse
        {
            get => _model.Strasse;
            set
            {
                if (_model.Strasse != value)
                {
                    _model.Strasse = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Ort
        {
            get => _model.Ort;
            set
            {
                if (_model.Ort != value)
                {
                    _model.Ort = value;
                    OnPropertyChanged();
                }
            }
        }

        public ProjektEditViewModel(IDataService<Projekt> dataService, IRenavigator renavigator, IActualState actualState)
        {
            _dataService = dataService;
            _renavigator = renavigator;

            ProjektID = actualState.ProjektID;

            _model = new Projekt();
            Speichern = new RelayCommand((x) => this.SaveProject());

            LoadProjekt();
        }

        private async void LoadProjekt()
        {

            _model = await _dataService.Get(ProjektID);
            if(_model == null)
            {
                _model = new Projekt()
                {
                    Auftraggeber = new Auftraggeber(),
                };
            }
            OnPropertyChanged(nameof(ProjektName));
            OnPropertyChanged(nameof(Erstelldatum));
            OnPropertyChanged(nameof(Strasse));
            OnPropertyChanged(nameof(Ort));
        }

        private void SaveProject()
        {
            _dataService.Update(_model);

            _renavigator.Renavigate();
        }
    }
}
