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

namespace UtopiaAttService
{
    class RegisteredUsers
    {
        public DataTable Registered_Users(string empId, MySqlConnection connection)
        {
            DataTable dataTable = new DataTable();
            string query = "Select amu.serial_number , am.machine_name , am.machine_ip from attendance_machine_user amu , attendance_machine am where amu.serial_number = @SerialNumber and amu.machine_id = am.machine_id and amu.is_deleted = 0;";
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@SerialNumber", empId);
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
            mySqlDataAdapter.SelectCommand = cmd;
            mySqlDataAdapter.Fill(dataTable);
            return dataTable;
        }

        public DataTable GetTemplateEmpwise(string empId, MySqlConnection connection)
        {
            DataTable dataTable = new DataTable();
            string query = "Select * from attendance_machine_face_templates a where a.serial_no = @SerialNumber and is_active = 1 ";
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@SerialNumber", empId);
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
            mySqlDataAdapter.SelectCommand = cmd;
            mySqlDataAdapter.Fill(dataTable);
            return dataTable;
        }


        public DataTable GetFaceTemplates( MySqlConnection connection, string empIds)
        {
            DataTable dataTable = new DataTable();

            // Split the empIds string into individual values
            string[] empIdArray = empIds.Split(',');

            // Create a list of parameter placeholders for each value
            List<string> parameterPlaceholders = new List<string>();
            for (int i = 0; i < empIdArray.Length; i++)
            {
                string paramName = "@SerialNumber" + i;
                parameterPlaceholders.Add(paramName);
            }

            // Create the IN clause for the query using the parameter placeholders
            string inClause = string.Join(",", parameterPlaceholders);

            // Create the SQL query with the IN clause
            string query = $"SELECT serial_no, template FROM attendance_machine_face_templates WHERE serial_no IN ({inClause}) AND is_active = 1";

            MySqlCommand cmd = new MySqlCommand(query, connection);

            // Add parameter values for each individual value
            for (int i = 0; i < empIdArray.Length; i++)
            {
                string paramName = "@SerialNumber" + i;
                cmd.Parameters.AddWithValue(paramName, empIdArray[i]);
            }

            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
            mySqlDataAdapter.SelectCommand = cmd;
            mySqlDataAdapter.Fill(dataTable);
            return dataTable;
        }

    }
}
