using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Masters_MapClockIn : Base.BasePage
{
    #region Variable Declaration
    ResourceManager rm;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    CultureInfo ci;
    ShiftExchangeEventData objShiftExchange = new ShiftExchangeEventData();
    PunchTypesEventdata objpunchType = new PunchTypesEventdata();
    EmployeeEventData objEmployee = new EmployeeEventData();
    string EmployeeId;
    DataSet ds_Checkuser = new DataSet();
    DataSet ds_CheckPunchTypes = new DataSet();
    int intShiftId = 0;
    int clkIn = 0, clkOut = 0, bkIn = 0, bkOut = 0, lunIn = 0, lunOut = 0;
    ManualEntryEventData objManualEntry = new ManualEntryEventData();
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        UserRights();
        lblCurrentDate.Text = DateTime.Now.ToString(Convert.ToString(Session["MDateFormat"]) + " HH:mm:ss tt");
        if (Convert.ToString(Session["EmployeeCodeId"]) != "")
        {
            DisableAndEnableControls();
        }
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(266, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        //ToDo: I need to change this condition after completion of development 
        if (ds_Checkuser.Tables[0].Rows.Count != 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void DisableAndEnableControls()
    {
        //ToDo: I need to change this condition after completion of development 
        if (Convert.ToString(Session["EmployeeCodeId"]) == "18000000")
        {
            lblempidvalue.Text = Convert.ToString(Session["EmployeeCodeId"]);
            EmployeeId = Convert.ToString(Session["EmpAutoID"]);
            ds_CheckPunchTypes = objpunchType.CheckingPunchTypes_Available();
            if (ds_CheckPunchTypes.Tables.Count > 0)
            {
                if (ds_CheckPunchTypes.Tables[0].Rows.Count > 0)
                {
                    if (Convert.ToInt16(ds_CheckPunchTypes.Tables[0].Rows[0]["ShiftIn"]) != 0)
                        lnkbtnClockIn.Visible = true;
                    if (Convert.ToInt16(ds_CheckPunchTypes.Tables[0].Rows[0]["ShiftOut"]) != 0)
                        lnkbtnClockOut.Visible = true;
                    if (Convert.ToInt16(ds_CheckPunchTypes.Tables[0].Rows[0]["LunchOut"]) != 0)
                        lnkbtnLunchOut.Visible = true;
                    if (Convert.ToInt16(ds_CheckPunchTypes.Tables[0].Rows[0]["LunchIn"]) != 0)
                        lnkbtnLunchIn.Visible = true;
                    if (Convert.ToInt16(ds_CheckPunchTypes.Tables[0].Rows[0]["BreakOut"]) != 0)
                        lnkbtnBreakOut.Visible = true;
                    if (Convert.ToInt16(ds_CheckPunchTypes.Tables[0].Rows[0]["BreakIn"]) != 0)
                        lnkbtnBreakIn.Visible = true;
                }
            }
            else
                DisableBtnControls();
        }
        else
            DisableBtnControls();
    }

    private void DisableBtnControls()
    {
        lnkbtnClockOut.Visible = false;
        lnkbtnClockIn.Visible = false;
        lnkbtnLunchOut.Visible = false;
        lnkbtnLunchIn.Visible = false;
        lnkbtnBreakIn.Visible = false;
        lnkbtnBreakOut.Visible = false;
    }

    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("WebClockInOut_PageHeader", ci);
        lblempid.Text = rm.GetString("email_EmployeeId", ci);
        lblCurrentloc.Text = rm.GetString("WebClockInOut_CurrentLocation", ci);
        lblcurrentdatTime.Text = rm.GetString("WebClockInOut_CurrentDateAndTime", ci);
        lbllastpunch.Text = rm.GetString("WebClockInOut_LastPunch", ci);
        lnkbtnBreakIn.Text = rm.GetString("PunchType_BreakIn", ci);
        lnkbtnBreakOut.Text = rm.GetString("PunchType_BreakOut", ci);
        lnkbtnClockIn.Text = rm.GetString("WebClockInOut_ClockIn", ci);
        lnkbtnClockOut.Text = rm.GetString("WebClockInOut_ClockOut", ci);
        lnkbtnLunchIn.Text = rm.GetString("PunchType_LunchIn", ci);
        lnkbtnLunchOut.Text = rm.GetString("PunchType_LunchOut", ci);
        //ToDo: I need to change this condition after completion of development 
        lblResult.InnerText = "Coimbatore";
        lblLastPunchval.Text = DateTime.Now.AddHours(-1).AddMinutes(-10).ToString();
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        LinkButton lnbtn = (LinkButton)sender;
        string strInDate = string.Empty, strOutDate = string.Empty, strManualTimeEntryStatus = string.Empty;
        if (lnbtn != null)
        {
            if (lblempidvalue.Text == "")
            {
                Message("Employee ID should not empty");
                return;
            }
            else if (lblResult.InnerText == "")
            {
                Message("Current Location Should not be empty. Please refresh your browser");
                return;
            }
            intShiftId = objShiftExchange.SelectShiftId(EmployeeId, DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString());
            if (intShiftId == 0)
            {
                DisableBtnControls();
                Message(rm.GetString("Msg_ManualTimeEntry", ci));
                return;
            }
            else
            {
                string punchType = "";
                switch (lnbtn.CommandName)
                {
                    case "Clock In":
                        punchType = "In";
                        break;
                    case "Clock Out":
                        punchType = "Out";
                        break;
                    case "Lunch Out":
                        punchType = "Lunch Out";
                        break;
                    case "Lunch In":
                        punchType = "Lunch In";
                        break;
                    case "Break Out":
                        punchType = "Break Out";
                        break;
                    case "Break In":
                        punchType = "Break In";
                        break;
                }
                if (Convert.ToString(lblempidvalue.Text) != "")
                {
                    int intCompanyId = base.GetCompanyId(Convert.ToInt32(Session["EmpAutoId"]));
                    DataSet dsApprovalSettings = new DataSet();
                    ShiftEvenData objShift = new ShiftEvenData();
                    dsApprovalSettings = objShift.SelectApprovalSettings(intCompanyId);
                    if (dsApprovalSettings != null)
                    {
                        if (dsApprovalSettings.Tables.Count > 0)
                        {
                            if (dsApprovalSettings.Tables[0].Rows.Count > 0)
                            {
                                if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["webClockInOut"]) == false)
                                    strManualTimeEntryStatus = "Approved";
                                else
                                    strManualTimeEntryStatus = GetManualTimeEntryStatus(Convert.ToString(lblempidvalue.Text));
                            }
                            else
                                strManualTimeEntryStatus = GetManualTimeEntryStatus(Convert.ToString(lblempidvalue.Text));
                        }
                    }
                    else
                        strManualTimeEntryStatus = "Approved";


                    if (punchType == "Clock In" || punchType == "Lunch In" || punchType == "Break In")
                    {
                        objManualEntry.InsertManualEntry
                        (
                            Convert.ToString(lblempidvalue.Text),
                            DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy")),
                            DateTime.Now.ToString("MM/dd/yyyy"),
                            DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
                            punchType,
                            "",
                            "",
                            "",
                            787,
                            0,
                            "Web " + punchType + " from" + Convert.ToString((lblCurrentloc.Text)),
                            0,
                            strManualTimeEntryStatus,
                            DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
                            Convert.ToInt32(Session["EmpAutoID"]),
                           Convert.ToString(ViewState["YearSettings_Id"]), 0, 0
                           );
                    }
                    else
                    {
                        objManualEntry.InsertManualEntry
                         (
                             Convert.ToString(lblempidvalue.Text),
                             DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy")),
                             "",
                             "",
                             "",
                             DateTime.Now.ToString("MM/dd/yyyy"),
                             DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
                             punchType,
                             0,
                             788,
                             "Web " + punchType + " from" + Convert.ToString((lblCurrentloc.Text)),
                             0,
                             strManualTimeEntryStatus,
                             DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
                             Convert.ToInt32(Session["EmpAutoID"]),
                             Convert.ToString(ViewState["YearSettings_Id"]), 0, 0
                            );
                    }
                }
            }
        }
    }
    public string GetManualTimeEntryStatus(string inEmployeeCodeId)
    {
        DataSet dsReportingPersons = new DataSet();
        string strManualTimeEntryStatus = string.Empty;
        Int64 intReportTo = 0, intLeave_Sanc_Authority = 0;
        dsReportingPersons = objEmployee.GetReportingToSanctionAuthorityByCodeID(inEmployeeCodeId);
        if (dsReportingPersons.Tables.Count > 0)
        {
            if (dsReportingPersons.Tables[0].Rows.Count > 0)
            {
                if (dsReportingPersons.Tables[0].Rows[0]["Reporting_To"] != DBNull.Value && dsReportingPersons.Tables[0].Rows[0]["Reporting_To"].ToString() != string.Empty)
                    intReportTo = Int64.Parse(dsReportingPersons.Tables[0].Rows[0]["Reporting_To"].ToString());
                if (dsReportingPersons.Tables[0].Rows[0]["Leave_Sanc_Authority"] != DBNull.Value && dsReportingPersons.Tables[0].Rows[0]["Leave_Sanc_Authority"].ToString() != string.Empty)
                    intLeave_Sanc_Authority = Int64.Parse(dsReportingPersons.Tables[0].Rows[0]["Leave_Sanc_Authority"].ToString());
            }
        }
        if (intReportTo != 0 && intLeave_Sanc_Authority != 0 && intReportTo != intLeave_Sanc_Authority)
            strManualTimeEntryStatus = "Pending";
        else
            strManualTimeEntryStatus = "Forwarded";
        return (strManualTimeEntryStatus);
    }
}