using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZktecoAttendenceService
{
    class AttendanceMachineDAO
    {

        public void update(AttendanceMachine attendanceMachine, MySqlConnection connection)
        {
           
            string query = "UPDATE `hrms`.`attendance_machine` SET `machine_ip` = @machine_ip, `port_number` = @port_number, `machine_name` = @machine_name, `site_id` = @site_id, `status` = @status, `last_sync_date` = @last_sync_date  WHERE `machine_id` = @machine_id";
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@machine_ip", attendanceMachine.MachineIp);
            cmd.Parameters.AddWithValue("@port_number", attendanceMachine.PortNumber);
            cmd.Parameters.AddWithValue("@machine_name", attendanceMachine.MachineName);
            cmd.Parameters.AddWithValue("@site_id", attendanceMachine.SiteId);
            cmd.Parameters.AddWithValue("@status", attendanceMachine.Status);
            cmd.Parameters.AddWithValue("@last_sync_date", attendanceMachine.LastSyncDate);
            cmd.Parameters.AddWithValue("@machine_id", attendanceMachine.MachineId);

            cmd.ExecuteNonQuery();
        
        }
        public void updateColony(AttendanceMachine attendanceMachine, MySqlConnection connection)
        {
            string query = "UPDATE `hrms`.`colony_attendance_machine` SET `machine_ip` = @machine_ip, `port_number` = @port_number, `machine_name` = @machine_name, `site_id` = @site_id, `status` = @status, `last_sync_date` = @last_sync_date WHERE `machine_id` = @machine_id";
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@machine_ip", attendanceMachine.MachineIp);
            cmd.Parameters.AddWithValue("@port_number", attendanceMachine.PortNumber);
            cmd.Parameters.AddWithValue("@machine_name", attendanceMachine.MachineName);
            cmd.Parameters.AddWithValue("@site_id", attendanceMachine.SiteId);
            cmd.Parameters.AddWithValue("@status", attendanceMachine.Status);
            cmd.Parameters.AddWithValue("@last_sync_date", attendanceMachine.LastSyncDate);
            cmd.Parameters.AddWithValue("@machine_id", attendanceMachine.MachineId);

            cmd.ExecuteNonQuery();
        }
        public List<AttendanceMachine> getAttendanceMachines(MySqlConnection connection, String siteId)
        {
            List<AttendanceMachine> attendanceMachines = null;

            // Define your query for active machines of type 'ZKTECO'
            string query = "select * from hrms.attendance_machine where machine_status = 1 and machine_type = 'ZKTECO'";

            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@siteId", siteId);

            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                attendanceMachines = getAttendanceMachineList(reader);
                reader.Close();
            }

            // Manually add the machine with ID 100 and IP 192.168.50.8
            if (attendanceMachines == null)
            {
                attendanceMachines = new List<AttendanceMachine>();
            }
            attendanceMachines.Add(new AttendanceMachine
            {
                MachineId = "100",
                MachineIp = "192.168.50.8",
                PortNumber = 4370,
                             // Add other necessary fields if they exist
            });

            return attendanceMachines;
        }



        private List<AttendanceMachine> getAttendanceMachineList(MySqlDataReader reader)
        {
            List<AttendanceMachine> attendanceMachines = new List<AttendanceMachine>();
            while (reader.Read())
            {
                attendanceMachines.Add(toAttendanceMachine(reader));
            }
            return attendanceMachines;
        }

        private AttendanceMachine toAttendanceMachine(MySqlDataReader reader)
        {
            return new AttendanceMachine(
                    reader.IsDBNull(0) ? "" : reader.GetString("machine_id"),
                    reader.IsDBNull(1) ? "" : reader.GetString("machine_ip"),
                    reader.IsDBNull(2) ? default(int) : reader.GetInt16("port_number"),
                    reader.IsDBNull(3) ? "" : reader.GetString("machine_name"),
                    reader.IsDBNull(4) ? default(int) : reader.GetInt16("site_id"),
                    reader.IsDBNull(5) ? "" : reader.GetString("status"),
                    reader.IsDBNull(6) ? default(DateTime) : reader.GetDateTime("last_sync_date")

                );
        }



    }
}
