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

namespace DaireApplication.DataBase
{
    public class ScreeenTable
    {
        public int Id { get; set; }
        public int brightness { get; set; }
        public float dimSec { get; set; }
        public float offSec { get; set; }
        public string port { get; set; }
        public int boundRate { get; set; }
        public int stopBits { get; set; }
        public int parity { get; set; }
        public int sendingTime { get; set; }
        public double warningLimit { get; set; }
        public double errorLimit { get; set; }

        public List<ScreeenTable>? ReadScreens()
        {
            try
            {
                string filePath = DataPathManager.GetDataFilePath("Screen.csv");

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

                string[] lines = File.ReadAllLines(filePath);
                List<ScreeenTable> screens = new();

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

                    if (columns.Length < 8)
                        continue;

                    screens.Add(new ScreeenTable
                    {
                        Id = int.Parse(columns[0]),
                        brightness = int.Parse(columns[1]),
                        dimSec = float.Parse(columns[2]),
                        offSec = float.Parse(columns[3]),
                        port = columns[4],
                        boundRate = int.Parse(columns[5]),
                        stopBits = int.Parse(columns[6]),
                        parity = int.Parse(columns[7]),
                        sendingTime = int.Parse(columns[8]),
                        warningLimit = double.Parse(columns[9]),
                        errorLimit = double.Parse(columns[10]),
                    });
                }

                return screens;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool UpdateScreen(ScreeenTable updatedScreen)
        {
            string filePath = DataPathManager.GetDataFilePath("Screen.csv");

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

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

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

                if (columns.Length < 8)
                    continue;

                if (int.Parse(columns[0]) == updatedScreen.Id)
                {
                    columns[1] = updatedScreen.brightness.ToString();
                    columns[2] = updatedScreen.dimSec.ToString();
                    columns[3] = updatedScreen.offSec.ToString();
                    columns[4] = updatedScreen.port;
                    columns[5] = updatedScreen.boundRate.ToString();
                    columns[6] = updatedScreen.stopBits.ToString();
                    columns[7] = updatedScreen.parity.ToString();
                    columns[8] = updatedScreen.sendingTime.ToString();
                    columns[9] = updatedScreen.warningLimit.ToString();
                    columns[10] = updatedScreen.errorLimit.ToString();

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

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

            return false;
        }
    }
}
