using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Unity.VisualScripting;

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

        public static bool ReadFile(string path)
        {
            if (!File.Exists(path))
                return false;
            string fileData = File.ReadAllText(path);//Encoding.GetEncoding(28605));
            string[] lines = fileData.Split("\n"[0]);
            int counter = 0;
            char separator = ',';
            foreach (string line in lines) {
                if (line == "")
                    continue;

                if (counter == 0) {
                    counter++;
                    if (line.Contains(';'))
                        separator = ';';
                    continue;
                }

                string[] lineData = line.Trim().Split(separator);
                ParkingPreference preference = lineData[0] == ""
                    ? ParkingPreference.Any
                    : lineData[1] == "P"
                        ? ParkingPreference.Front
                        : ParkingPreference.Back;
                Size size;
                string sizeString = lineData[2];
                List<int> chars = new();
                foreach (char c in sizeString) {
                    chars.Add((int)c);
                }
                if (sizeString == "Mały" || sizeString == "Ma" + (char)0xB3 + "y" || (sizeString.Contains("Ma") && sizeString.Contains("y"))) {
                    size = Size.A;
                }else if (sizeString == "Średni" || sizeString == (char)0xD7 + "redni" || sizeString.Contains("redni")) {
                    size = Size.B;
                }else if (sizeString == "Duży" || sizeString == "Du" + (char)0xBF + "y" || sizeString.Contains("Du")) {
                    size = Size.C;
                }else if (sizeString == "Max") {
                    size = Size.D;
                }
                else {
                    size = Size.A;
                }

                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 (!int.TryParse(lineData[0], out int priority))
                    priority = 3;

                if (!int.TryParse(lineData[7], out int updateInterval))
                    updateInterval = 3;

                Driver driver = new(size, counter, preference, priority, updateInterval);
                driver.Times = new[] {plannedArrival, plannedDeparture, realArrival, realDeparture};
                Drivers.Add(driver);

                counter++;
            }
            return true;
        }
    }
}