using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using Unity.VisualScripting;
using UnityEngine;

namespace Parking
{
    public class DataImporter
    {
        public static List<Driver> Drivers = new();

        public static void ReadFile(string path)
        {
            var fileData = File.ReadAllText(path);
            var lines = fileData.Split("\n"[0]);
            var counter = 0;
            foreach (var line in lines)
            {
                if(line == "")
                    continue;
                
                if (counter == 0)
                {
                    counter++;
                    continue;
                }

                var lineData = line.Trim().Split(',');
                var preference = lineData[0] == ""
                    ? ParkingPreference.Any
                    : lineData[1] == "P"
                        ? ParkingPreference.Front
                        : ParkingPreference.Back;
                Size size;
                switch (lineData[2])
                {
                    case "Mały":
                        size = Size.A;
                        break;
                    case "Średni":
                        size = Size.B;
                        break;
                    case "Duży":
                        size = Size.C;
                        break;
                    case "Max":
                        size = Size.D;
                        break;
                    default:
                        size = Size.A;
                        break;
                }

                DateTime realArrival = DateTime.Parse(lineData[5].Contains(':') ? lineData[5] : lineData[5] + ":00", CultureInfo.InvariantCulture);
                DateTime realDeparture = DateTime.Parse(lineData[6].Contains(':') ? lineData[6] : lineData[6] + ":00", CultureInfo.InvariantCulture);
                DateTime plannedArrival = DateTime.Parse(lineData[3].Contains(':') ? lineData[3] : lineData[3] + ":00", CultureInfo.InvariantCulture);
                DateTime plannedDeparture = DateTime.Parse(lineData[4].Contains(':') ? lineData[4] : lineData[4] + ":00", CultureInfo.InvariantCulture);

                if (!Int32.TryParse(lineData[0], out var priority))
                    priority = 3;

                if (!Int32.TryParse(lineData[7], out var updateInterval))
                    updateInterval = 3;
                
                Driver driver = new Driver(size, counter, preference, priority, updateInterval);
                driver.Times = new[] {plannedArrival, plannedDeparture, realArrival, realDeparture};
                Drivers.Add(driver);

                counter++;
            }
        }
    }
}