using SewerStammGen.Shared.Contracts;
using SewerStammGen.Shared.Domain;
using SewerStammGen.WPF.Commands;
using SewerStammGen.WPF.Interface.Navigator;
using SewerStammGen.WPF.ViewModel.State;
using Shared.Contracts;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace SewerStammGen.WPF.ViewModel
{
    internal class HaltungEditViewModel : BaseViewModel
    {
        private readonly IActualState _actualState;
        private readonly IHaltungDataService _haltungDataService;
        private readonly ISchachtDataService _schachtDataService;
        private List<Schacht> avaibleSchaechte;

        public List<Schacht> AvSchaechte
        {
            get
            {
                return avaibleSchaechte;
            }
        }

        int _selectedObenIndex;
        int _selectedUntenIndex;

        public int SelectedObenIndex
        {
            get => _selectedObenIndex;
            set
            {
                if (_selectedObenIndex != value)
                {
                    _selectedObenIndex = value;
                    _model.StartSchacht = avaibleSchaechte[value];
                    RecalculateLength();
                    OnPropertyChanged();
                }
            }
        }
        public int SelectedUntenIndex
        {
            get => _selectedUntenIndex;
            set
            {
                if (_selectedUntenIndex != value)
                {
                    _selectedUntenIndex = value;
                    _model.EndSchacht = avaibleSchaechte[value];
                    RecalculateLength();
                    OnPropertyChanged();
                }
            }
        }

        private Kanal _model;

        public Kanal Model
        {
            get => _model;
            set
            {
                _model = value;
            }
        }

        private void RecalculateLength()
        {

            double x1 = (double)Model.StartSchacht.RechtsWert;
            double x2 = (double)Model.EndSchacht.RechtsWert;
            double y1 = (double)Model.StartSchacht.HochWert;
            double y2 = (double)Model.EndSchacht.HochWert;


            double length = Math.Sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
            Haltungslaenge = length.ToString();
            OnPropertyChanged(nameof(Haltungslaenge));
        }
        public string Haltungsbezeichnung
        {
            get => _model.Objektbezeichnung;
            set
            {
                if (_model.Objektbezeichnung != value)
                {
                    _model.Objektbezeichnung = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Material
        {
            get => _model.Material;
            set
            {
                if (_model.Material != value)
                {
                    _model.Material = value;
                    OnPropertyChanged();
                }
            }
        }
        public string Durchmesser
        {
            get => _model.DN.ToString();
            set
            {
                if (_model.DN.ToString() != value)
                {
                    _model.DN = int.Parse(value);
                    OnPropertyChanged();
                }
            }
        }
        public string Haltungslaenge
        {
            get => _model.Haltungslaenge.ToString();
            set
            {
                if (_model.Haltungslaenge.ToString() != value)
                {
                    _model.Haltungslaenge = decimal.Parse(value);
                    OnPropertyChanged();
                }
            }
        }

        public ICommand Speichern { get; set; }
        public ICommand Abbrechen { get; set; }

        public HaltungEditViewModel(
            IHaltungDataService haltungDataService,
            IActualState actualState,
            IRenavigator renavigator,
            ISchachtDataService schachtDataService)
        {
            _actualState = actualState;
            _haltungDataService = haltungDataService;
            _schachtDataService = schachtDataService;



            _model = _actualState.SelectedHaltung;
            
            Speichern = new HaltungEditSaveCommand(_haltungDataService, renavigator, this);
            Abbrechen = new RelayCommand((x) => Abbruch(renavigator));

            ladeVerfuegbareSchaechte();



        }

        private void Abbruch(IRenavigator renavigator)
        {
            renavigator.Renavigate();
        }

        private async void ladeVerfuegbareSchaechte()
        {
            var s = await _schachtDataService.GetAllByProjekt(_actualState.ProjektID);
            avaibleSchaechte = s.ToList();
            int counter = 0;
            foreach (var d in avaibleSchaechte)
            {
                if (d.Id == Model.StartSchacht.Id)
                {
                    SelectedObenIndex = counter;
                    break;
                }
                counter++;
            }
            counter = 0;
            foreach (var d in avaibleSchaechte)
            {
                if (d.Id == Model.EndSchacht.Id)
                {
                    SelectedUntenIndex = counter;
                    break;
                }
                counter++;
            }
            OnPropertyChanged(nameof(AvSchaechte));
            OnPropertyChanged(nameof(SelectedObenIndex));
            OnPropertyChanged(nameof(SelectedUntenIndex));
        }
    }
}
