using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;

namespace eDosStation
{
    public class Station
    {
        public int ID_Station { get; set; }
        public int ComPort { get; set; }
        public int Inst_CODE1 { get; set; }
        public int Inst_CODE2 { get; set; }
        public string Inst_Name1 { get; set; }
        public string Inst_Name2 { get; set; }
        public string TestCSN { get; set; }
        public string TestCode { get; set; }
        public string Reader { get; internal set; }

        public bool Changed = false;
        public int Error = 0;
        public string ErrorMessage;
        public StringBuilder insComments;
        public TextBlock tbComments;
        public DataInterface dataInterface;

        public Station(DataInterface mDataInterface, string Station)
        {
            dataInterface = mDataInterface;
            Error = 0;
            ErrorMessage = string.Empty;
            ComPort = 0;
            Inst_CODE1 = 1;
            Inst_CODE2 = 11;
            Inst_Name1 = "BR1";
            Inst_Name2 = "Venus";
            TestCSN = System.Configuration.ConfigurationManager.AppSettings["TestCSN"];
            TestCode = System.Configuration.ConfigurationManager.AppSettings["TestCode"];
            insComments = new StringBuilder();
            tbComments = new TextBlock();
            tbComments.FontSize = 20;
            tbComments.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            tbComments.TextAlignment = System.Windows.TextAlignment.Center;

            var co = dataInterface.getCon(true);  // No dispose
            {
                using (var cmd = new SqlCommand("StationSettingsSelect", co))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    try
                    {
                        var rs = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow);
                        if (rs.HasRows && rs.Read())
                        {
                            object[] Values = new object[rs.FieldCount];
                            int rv = rs.GetValues(Values);
                            ID_Station = (int)Values[rs.GetOrdinal("ID_Station")];
                            ComPort = (int)Values[rs.GetOrdinal("ComPort")];
                            Inst_CODE1 = (int)Values[rs.GetOrdinal("Inst_CODE1")];
                            Inst_CODE2 = (int)Values[rs.GetOrdinal("Inst_CODE2")];
                            Inst_Name1 = Values[rs.GetOrdinal("Inst_Name1")].ToString();
                            Inst_Name2 = Values[rs.GetOrdinal("Inst_Name2")].ToString();
                            TestCSN = Values[rs.GetOrdinal("TestCSN")].ToString();
                            Reader = Values[rs.GetOrdinal("Reader")].ToString();
                        }
                        else
                        {
                            ID_Station = 0;
                            ComPort = 0;
                            Inst_CODE1 = 1;
                            Inst_CODE2 = 11;
                            Inst_Name1 = "BR1"; Inst_Name2 = "Venus";
                            TestCSN = System.Configuration.ConfigurationManager.AppSettings["TestCSN"];
                            TestCode = System.Configuration.ConfigurationManager.AppSettings["TestCode"];
                            Changed = true;
                            Reader = System.Configuration.ConfigurationManager.AppSettings["Reader"];
                        }
                        rs.Close();
                    }
                    catch (System.Exception ex)
                    {
                        Error = 1;
                        ErrorMessage = ex.Message;
                    }
                }

                dataInterface.SetStation(ID_Station);

                if (Error == 0) using (var cmd2 = new SqlCommand("GetInstallationsComments", co))
                    {
                        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd2.Parameters.AddWithValue("@Inst_CODE1", Inst_CODE1);
                        if (Inst_CODE2 > 0) cmd2.Parameters.AddWithValue("@Inst_CODE2", Inst_CODE2);
                        try
                        {
                            var rs = cmd2.ExecuteReader(System.Data.CommandBehavior.SingleResult);
                            string curins = string.Empty, thisins = string.Empty, thiscomment = string.Empty;
                            if (rs.HasRows)
                                while (rs.Read())
                                {
                                    thisins = rs.GetString(2);
                                    thiscomment = rs.GetString(3).Replace("\\n", "\n");
                                    if (curins.CompareTo(thisins) != 0)
                                    {
                                        if (curins != string.Empty) tbComments.Inlines.Add(new LineBreak());
                                        tbComments.Inlines.Add(new Bold(new Run($"Installation {thisins}:\n") { FontSize = 25 }));
                                        insComments.AppendLine($"Installation {thisins}");
                                        curins = thisins;
                                    }
                                    tbComments.Inlines.Add(new Run(thiscomment));
                                }
                            rs.Close();
                        }
                        catch (System.Exception ex)
                        {
                            Error = 2;
                            ErrorMessage = ex.Message;
                        }
                    }
                co?.Close();
            }
        }


        private void UpdateToDb(SqlConnection co, SqlCommand cmd)
        {
            cmd.Connection = co;
            cmd.ExecuteNonQuery();
        }
        public void Update()
        {

            SqlConnection co=null;
            using (var cmd = new SqlCommand("StationSettingsUpdate"))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("ComPort", ComPort);

                if (!string.IsNullOrEmpty(Inst_Name1) && Inst_CODE1 > 0)
                {
                    cmd.Parameters.AddWithValue("Inst_CODE1", Inst_CODE1);
                    cmd.Parameters.AddWithValue("Inst_Name1", Inst_Name1);
                }

                if (!string.IsNullOrEmpty(Inst_Name2) && Inst_CODE2 > 0)
                {
                    cmd.Parameters.AddWithValue("Inst_CODE2", Inst_CODE2);
                    cmd.Parameters.AddWithValue("Inst_Name2", Inst_Name2);
                }
                if (!string.IsNullOrEmpty(TestCSN))
                    cmd.Parameters.AddWithValue("TestCSN", TestCSN);

                if (!string.IsNullOrEmpty(Reader))
                    cmd.Parameters.AddWithValue("@Reader", Reader);

                try
                {
                    co = dataInterface.getCon(true);
                    UpdateToDb(co, cmd);
                    co.Close();
                    co = dataInterface.getConCentral(true);
                    UpdateToDb(co, cmd);
                }
                catch (System.Exception ex)
                {
                    Error = 1;
                    ErrorMessage = ex.Message;
                }
                co?.Close();
            }

        }
    }
}

