using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HRM.BO;
using Microsoft.AspNetCore.Http;
using System.Data;
using System.Reflection;

namespace HRM.UI.Controllers.Mobile
{
    [Route("api/MobileAuthentication")]
    [ApiController]
    public class MobileAuthenticationController : ControllerBase
    {
        private T GetItem<T>(DataRow dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            foreach (DataColumn column in dr.Table.Columns)
            {
                foreach (PropertyInfo pro in temp.GetProperties())
                {
                    if (pro.Name == column.ColumnName)
                        pro.SetValue(obj, dr[column.ColumnName], null);
                    else
                        continue;
                }
            }
            return obj;
        }
        public readonly IEmpMobileService _empMobileService;

        public MobileAuthenticationController(
            IEmpMobileService empMobileService)
        {
            this._empMobileService = empMobileService;
        }

        [HttpGet("getAllDevice")]
        public ActionResult getAllDevice()
        {
            List<EmpMobile> items = new List<EmpMobile>();
            try
            {
                items = _empMobileService.Get();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok(items);
        }

        [HttpGet("getActiveDevice")]
        public ActionResult getActiveDevice()
        {
			DataSet dset = new DataSet();
            try
            {
                dset = _empMobileService.getActiveDevice(true);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
            var lst = dset.Tables[0].AsEnumerable()
                    .Select(dataRow => new
                    {
                        empNo = dataRow.Field<string>("EMPLOYEENO"),
                        empName = dataRow.Field<string>("NAME"),
                        empMobID = dataRow.Field<int>("EmpMobID"),
                        deviceNo = dataRow.Field<string>("DeviceNo"),
                        creationDate = dataRow.Field<DateTime>("CREATIONDATE").ToString("dd MMM yyyy"),
                        isActive = dataRow.Field<int>("IsActive") == 1 ? "Active" : "InActive",
                    }).ToList();
            return Ok(lst);
        }

        [HttpGet("getInActiveDevice")]
        public ActionResult getInActiveDevice()
        {
            DataSet dset = new DataSet();
            try
            {
                dset = _empMobileService.getActiveDevice(false);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
            var lst = dset.Tables[0].AsEnumerable()
                    .Select(dataRow => new
                    {
                        empNo = dataRow.Field<string>("EMPLOYEENO"),
                        empName = dataRow.Field<string>("NAME"),
                        empMobID = dataRow.Field<int>("EmpMobID"),
                        deviceNo = dataRow.Field<string>("DeviceNo"),
                        creationDate = dataRow.Field<DateTime>("CREATIONDATE").ToString("dd MMM yyyy"),
                        isActive = dataRow.Field<int>("IsActive") == 1 ? "Active" : "InActive",
                    }).ToList();
            return Ok(lst);
        }

        [HttpPost]
        [Route("delete")]
        public ActionResult delete(EmpMobile pEmpMobile)
        {
            try
            {
                _empMobileService.Delete(pEmpMobile);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        [HttpPost]
        [Route("deleteDevice")]
        public ActionResult deleteDevice(dynamic data)
        {
            List<Objective> items = new List<Objective>();
            var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
            string ids = (string)item["ids"].ToObject<string>();
            try
            {
                _empMobileService.Delete(ids);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        [HttpPost]
        [Route("Save")]
        public ActionResult Save(EmpMobile item)
        {
            try
            {
                _empMobileService.Save(item);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }

        [HttpPost]
        [Route("SaveDevice")]
        public ActionResult SaveDevice(dynamic data)
        {
            List<Objective> items = new List<Objective>();
            var item = Newtonsoft.Json.JsonConvert.DeserializeObject(Convert.ToString(data));
            string ids = (string)item["ids"].ToObject<string>();
            try
            {
                _empMobileService.Save(ids);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }

            return Ok();
        }
    }
}