using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InsiderAttackdetector.PM4Py;


namespace InsiderAttackdetector
{
    public class Commander
    {
        public async Task StartProgram()
        {
            while (true)
            {
                Console.WriteLine("Choose action: "
                    + "\n [1] Start logging"
                    + "\n [2] Start logging and Process Mining"
                    + "\n [3] Perform Process Mining");

                var input = Console.ReadLine();

                if (int.TryParse(input, out var commandNumber) && (commandNumber <= 3) && (commandNumber >= 1))
                {
                    switch (commandNumber)
                    {
                        case 1:
                            await StartLogging();
                            break;
                        case 2:
                            {
                                var logPath = await StartLogging();
                                RunProcessMining(logPath);
                                break;
                            }
                            
                        case 3:
                            {
                                var logPath = GetProcessMiningData();
                                RunProcessMining(logPath);
                                break;
                            }
                    }
                    break;
                }

                Console.WriteLine("Wrong command! Try again.");
            }
        }


        private void RunProcessMining(string logPath)
        {
            Console.WriteLine("Starting Process Mining...");
            PMCommander.RunProcessMining(logPath);
            Console.WriteLine("Finished...");
        }

        
        private string GetProcessMiningData()
        {
            var logFiles = Helpers.GetLogFilePaths();
            
            if (!logFiles.Any()) 
            {
                Console.WriteLine("No log files in selected directory!");
            }

            var logCLIOutput = Helpers.GetStringFromLogFilePaths(logFiles);

            while (true)
            {
                Console.WriteLine("Choose logfile:\n"
                    + logCLIOutput);

                var input = Console.ReadLine();

                if (int.TryParse(input, out var fileNumber) && (fileNumber < logFiles.Count) && (fileNumber >= 0))
                {
                    return logFiles[fileNumber];
                }

                Console.WriteLine("Wrong command! Try again.");
            }
        }
        

        private async Task<string> StartLogging()
        {
            Console.WriteLine("Initializing logger...");
            string logPath;
            using (var captureInitializer = new DataGatherer())
            {
                Console.WriteLine("Starting logger...");
                logPath = await captureInitializer.Capture();
                Console.WriteLine("Ending logging...");
            }

            return logPath;
        }
    }
}
