using SewerStammGen.Shared.Contracts;
using SewerStammGen.Shared.Domain;
using Shared.Contracts;
using StammGenerator.Commands;
using StammGenerator.Interface;
using System.Windows.Input;

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

        public ProjektSettingsViewModel ProjektSettingsViewModel { get; set; }

        public ICommand Speichern { get; set; }
        public ICommand SchachtImportCommand { get; set; }
        public ICommand DatenExportCommand { 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(IProjektDataService dataService,IHaltungDataService haltungDataService, ISchachtDataService schachtDataService,  IActualState actualState, IRenavigator renavigator, IRenavigator navigatetoImport)
        {
            _dataService = dataService;
            _renavigator = renavigator;

            ProjektID = actualState.ProjektID;

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


            LoadProjekt();

            SchachtImportCommand = new RelayCommand((x) =>
            {
                navigatetoImport.Renavigate();
            });

            DatenExportCommand = new ProjectExportCommand(actualState, haltungDataService, schachtDataService, dataService);
            
        }

        private async void LoadProjekt()
        {

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

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

            _renavigator.Renavigate();
        }
    }
}
