using SewerStammGen.Shared.Contracts;
using SewerStammGen.Shared.Domain;
using SewerStammGen.Shared.Enum;
using Shared.Contracts;
using StammGenerator.Interface;
using StammGenerator.Services;
using System.Windows.Input;
using WWTech_KanalSchnittstelle.Importer;

namespace StammGenerator.ViewModel
{
    public class ManholeImportViewModel : BaseViewModel
    {
        private readonly ISchachtDataService schachtDataService;
        private readonly IRenavigator renavigator;
        private OpenFileDialogService fileDialogService;
        
        private string filename = string.Empty;
        public string FileName
        {
            get => filename;
            set
            {
                if (filename == value) return;
                filename = value;
                OnPropertyChanged();
            }
        }

        public ICommand LoadFile { get; set; }
        public ICommand OpenFileDialogCommand { get; set; }

        private readonly IImport importer;

        public ManholeImportViewModel(ISchachtDataService schachtDataService, IRenavigator renavigator, IActualState actualState)
        {
#if DEBUG
            FileName = @"C:\Users\damia\source\repos\Stammdatengenerator\Beispieldaten\Koordinatendatei.csv";
#endif
            LoadFile = new RelayCommand((x) => importFile());
            this.schachtDataService = schachtDataService;

            importer = new CSVImporter(actualState.ProjektID);
            this.renavigator = renavigator;
            this.fileDialogService = new OpenFileDialogService();
            this.OpenFileDialogCommand = new RelayCommand((x) =>
            {
                FileName = fileDialogService.OpenFileDialog();
            });
        }

        private async void importFile()
        {
            var schaechte = importer.LoadSchaechte(FileName, "Marwede");
            if (schaechte != null)
            {
               await schachtDataService.InsertSchachtBulk(schaechte);
            }
            renavigator.Renavigate();
        }
    }
}
