using AvaloniaApplication1.DataBase;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace DaireApplication.DataBase
{
    public class MachineTable
    {
        public int Id { get; set; }
        public float TankMaxHeat { get; set; } = 50;
        public float PumbMaxHeat { get; set; } = 50;
        public int PumbDelay { get; set; } = 60;
        public int MixerDelay { get; set; } = 60;
        public int HeatingDelay { get; set; } = 60;
        public int CoolingDelay { get; set; } = 60;
        public int PouringDelay { get; set; } = 60;
        public float PumbMinHeat { get; set; } = -10;
        public float AbsMaxHeat { get; set; } = 65;
        public float AbsMinHeat { get; set; } = -14;
        public float PreHeatingTemp { get; set; } = 5;
        public float setTemp1 { get; set; } =0;
        public float setTemp2 { get; set; } =-10;
        public float setTemp3 { get; set; } =+5;
        public float setTemp4 { get; set; } =+0;

        public MachineTable ReadMachine()
        {
            string filePath = DataPathManager.GetDataFilePath("Machine.csv");
            MachineTable machine = new MachineTable();

            if (File.Exists(filePath))
            {
                string[] lines = File.ReadAllLines(filePath);

                foreach (string line in lines)
                {
                    string[] columns = line.Split(',');

                    if (columns[0] == "1")
                    {
                        machine.Id = int.Parse(columns[0]);
                        machine.TankMaxHeat = float.Parse(columns[1]);
                        machine.PumbMaxHeat = float.Parse(columns[2]);
                        machine.PumbDelay = int.Parse(columns[3]);
                        machine.MixerDelay = int.Parse(columns[4]);
                        machine.HeatingDelay = int.Parse(columns[5]);
                        machine.CoolingDelay = int.Parse(columns[6]);
                        machine.PouringDelay = int.Parse(columns[7]);
                        machine.PumbMinHeat = int.Parse(columns[8]);
                        machine.AbsMaxHeat = int.Parse(columns[9]);
                        machine.AbsMinHeat = int.Parse(columns[10]);
                        machine.PreHeatingTemp = float.Parse(columns[11]);
                        machine.setTemp1 = float.Parse(columns[12]);
                        machine.setTemp2 = float.Parse(columns[13]);
                        machine.setTemp3 = float.Parse(columns[14]);
                        machine.setTemp4 = float.Parse(columns[15]);
                        return machine;
                    }
                }
                return null;
            }
            else
            {
                return null;
            }
        }

        public bool UpdateMachine(MachineTable updatedMachine)
        {
            string filePath = DataPathManager.GetDataFilePath("Machine.csv");

            if (!File.Exists(filePath))
                return false;

            string[] lines = File.ReadAllLines(filePath);
            bool machineFound = false;

            for (int i = 1; i < lines.Length; i++)
            {
                string[] columns = lines[i].Split(',');

                if (columns.Length < 8)
                    continue;

                if (int.Parse(columns[0]) == updatedMachine.Id)
                {
                    columns[1] = updatedMachine.TankMaxHeat.ToString(CultureInfo.InvariantCulture);
                    columns[2] = updatedMachine.PumbMaxHeat.ToString(CultureInfo.InvariantCulture);
                    columns[3] = updatedMachine.PumbDelay.ToString();
                    columns[4] = updatedMachine.MixerDelay.ToString();
                    columns[5] = updatedMachine.HeatingDelay.ToString();
                    columns[6] = updatedMachine.CoolingDelay.ToString();
                    columns[7] = updatedMachine.PouringDelay.ToString();
                    columns[8] = updatedMachine.PumbMinHeat.ToString(CultureInfo.InvariantCulture);
                    columns[9] = updatedMachine.AbsMaxHeat.ToString(CultureInfo.InvariantCulture);
                    columns[10] = updatedMachine.AbsMinHeat.ToString(CultureInfo.InvariantCulture);
                    columns[11] = updatedMachine.PreHeatingTemp.ToString(CultureInfo.InvariantCulture);
                    columns[12] = updatedMachine.setTemp1.ToString(CultureInfo.InvariantCulture);
                    columns[13] = updatedMachine.setTemp2.ToString(CultureInfo.InvariantCulture);
                    columns[14] = updatedMachine.setTemp3.ToString(CultureInfo.InvariantCulture);
                    columns[15] = updatedMachine.setTemp4.ToString(CultureInfo.InvariantCulture);

                    lines[i] = string.Join(",", columns);
                    machineFound = true;
                    break;
                }
            }

            if (machineFound)
            {
                File.WriteAllLines(filePath, lines);
                return true;
            }

            return false;
        }
    }
}
