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

namespace FaceTransfer
{
    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 List<AttendanceMachine> getAttendanceMachineDetails(MySqlConnection connection, string siteId , string machineIP)
        {
            string query;
            if (!siteId.Equals("0"))
            {
               
                query = "select * from hrms.attendance_machine where site_id = @siteId and machine_ip= @machineip";
            }
            else
            {
                query = "select * from hrms.attendance_machine where  machine_ip = @machineip";
            }
            List<AttendanceMachine> attendanceMachines = null;
           
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@siteId", siteId);
            cmd.Parameters.AddWithValue("@machineip", machineIP);


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

           
        }

        public (List<Attendance> employees, int totalCount) GetEmployees(MySqlConnection connection, string machineid)
        {
            string query = "SELECT serial_number FROM hrms.attendance_machine_user WHERE machine_id = @machine_id AND is_deleted = 0 ORDER BY CAST(serial_number AS UNSIGNED);";
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@machine_id", machineid);

            List<Attendance> emp = new List<Attendance>();
            int totalCount = 0;

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    string serialNumber = reader["serial_number"].ToString();

                    // Create a new Attendance object and set its SerialNo property
                    Attendance attendance = new Attendance();
                    attendance.AcNo = serialNumber;

                    // Add the Attendance object to the emp list
                    emp.Add(attendance);

                    // Increment the total count
                    totalCount++;
                }
            }

            // Return the list of employees and the total count as a tuple
            return (emp, totalCount);
        }
        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")

                );
        }



    }
}
