using System;
using System.Web.UI;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                              Created On  : 12-Aug-2007
* Module          : Shift Master                                           Modified On : 07-June-2007
* Created By      : Janakaraj.P
* Modified By     : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
 * 
 * Modified By      :   Manjula.P
 * Modified On      :   30-Sep-2013
 * Descriptions     :   Uncommented by Manjula.P on 30-Sep-2013 for getting Invalid break time for 01:01 to 01:31 case without break time availbale. Mantis : 0018989 
****************************************************************************************************************************/
public partial class Masters_ShiftRemote : Base.BasePage
{
    ShiftEvenData objShift = new ShiftEvenData();
    DateTime dt_ShiftIn, dt_Shiftout, dt_ShitBin1, dt_ShiftBout1, dt_ShiftBin2, dt_ShiftBout2, dt_ShiftBin3, dt_ShiftBout3;
    int Shiftinhr, shiftinmin, Shiftouthr, shiftoutmin, totalHorus, diff, TotalBreakHrs, BrkTime1inhr, BrkTime1inmin, BrkTime1outhr, BrkTime1outmin, BrkTime2inhr, BrkTime2inmin, BrkTime2outhr, BrkTime2outmin, BrkTime3inhr, BrkTime3inmin, BrkTime3outhr, BrkTime3outmin;
    //int ChkWorkinghrs,CheckBrk,checkTot,count,minot;
    int totalFHHours, totalSHHours;
    string Shiftin, Shiftout, BreakHrs, BStartTime1, BEndTime1, BStartTime2, BEndTime2, BStartTime3, BEndTime3, TempOutTime, TempInTime;
    string FHStart, SHStart, FHEnd, SHEnd;
    TimeSpan tsBreak1, tsBreak2, tsBreak3;
    int intbreakhrsMins = 0;
    bool ckbStaightShift = false;
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            if (Convert.ToString(Request.QueryString["ckbStaightShift"]) != "")
                ckbStaightShift = Convert.ToBoolean(Request.QueryString["ckbStaightShift"]);
            if (Convert.ToString(Request.QueryString["BreakHrs"]) != "")
            {
                BreakHrs = Convert.ToString(Request.QueryString["BreakHrs"]);
                string[] breakhrsMins = BreakHrs.Split(':');
                if (breakhrsMins[0] != null && breakhrsMins[1] != null)
                    intbreakhrsMins = Convert.ToInt16(breakhrsMins[0]) * 60 + Convert.ToInt16(breakhrsMins[1]);
            }
            if (Convert.ToString(Request.QueryString["Shiftin"]) != "")
                Shiftin = Convert.ToDateTime(Request.QueryString["Shiftin"]).ToString("hh:mm tt");
            if (Request.QueryString["Shiftout"].ToString() != "")
                Shiftout = Convert.ToDateTime(Request.QueryString["Shiftout"]).ToString("hh:mm tt");
            if (!ckbStaightShift)
            {
                if (Convert.ToString(Request.QueryString["BStartTime1"]) != "")
                    BStartTime1 = Convert.ToDateTime(Request.QueryString["BStartTime1"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["BEndTime1"]) != "")
                    BEndTime1 = Convert.ToDateTime(Request.QueryString["BEndTime1"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["BStartTime2"]) != "")
                    BStartTime2 = Convert.ToDateTime(Request.QueryString["BStartTime2"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["BEndTime2"]) != "")
                    BEndTime2 = Convert.ToDateTime(Request.QueryString["BEndTime2"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["BStartTime3"]) != "")
                    BStartTime3 = Convert.ToDateTime(Request.QueryString["BStartTime3"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["BEndTime3"]) != "")
                    BEndTime3 = Convert.ToDateTime(Request.QueryString["BEndTime3"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["FHStart"]) != "")
                    FHStart = Convert.ToDateTime(Request.QueryString["FHStart"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["FHEnd"]) != "")
                    FHEnd = Convert.ToDateTime(Request.QueryString["FHEnd"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["SHStart"]) != "")
                    SHStart = Convert.ToDateTime(Request.QueryString["SHStart"]).ToString("hh:mm tt");
                if (Convert.ToString(Request.QueryString["SHEnd"]) != "")
                    SHEnd = Convert.ToDateTime(Request.QueryString["SHEnd"]).ToString("hh:mm tt");
            }
            else
            {
                FHStart = Shiftin;
                FHEnd = Shiftout;
            }
            FindFirstHalfWorkHrs();
            if (!ckbStaightShift)
            {
                FindSecondHalfWorkHrs();
                FindTotalBreakTime();
            }
            else
                totalFHHours = (totalFHHours > 0) ? totalFHHours - intbreakhrsMins : 0;
            FindWorkingHours();
        }
    }
    private void FindFirstHalfWorkHrs()
    {
        if (FHStart != null && FHEnd != null && FHStart != "" && FHEnd != "")
        {
            dt_ShiftIn = Convert.ToDateTime(FHStart);
            dt_Shiftout = Convert.ToDateTime(FHEnd);
            Shiftinhr = dt_ShiftIn.Hour;
            shiftinmin = dt_ShiftIn.Minute;
            Shiftouthr = dt_Shiftout.Hour;
            shiftoutmin = dt_Shiftout.Minute;
            TempOutTime = FHEnd.Substring(6, 2);
            TempInTime = FHStart.Substring(6, 2);
            if (TempOutTime == "AM" && TempInTime == "PM")
            {
                diff = (Shiftouthr + 12) - (Shiftinhr - 12);
                Shiftouthr = Shiftouthr + 12;
                Shiftinhr = Shiftinhr - 12;
            }
            else if (TempOutTime == "PM" && TempInTime == "AM")
                diff = Shiftouthr - Shiftinhr;
            else if (TempOutTime == "AM" && TempInTime == "AM")
                diff = Shiftouthr - Shiftinhr;
            else if (TempOutTime == "PM" && TempInTime == "PM")
                diff = Shiftouthr - Shiftinhr;
            if (diff > 0)
                totalFHHours = ((Shiftouthr * 60) + shiftoutmin) - ((Shiftinhr * 60) + (shiftinmin));
            else if (diff == 0)
                totalFHHours = shiftoutmin - shiftinmin;
        }
    }
    private void FindSecondHalfWorkHrs()
    {
        if (SHStart != null && SHEnd != null && SHStart != "" && SHEnd != "")
        {
            dt_ShiftIn = Convert.ToDateTime(SHStart);
            dt_Shiftout = Convert.ToDateTime(SHEnd);
            Shiftinhr = dt_ShiftIn.Hour;
            shiftinmin = dt_ShiftIn.Minute;
            Shiftouthr = dt_Shiftout.Hour;
            shiftoutmin = dt_Shiftout.Minute;
            TempOutTime = SHEnd.Substring(6, 2);
            TempInTime = SHStart.Substring(6, 2);
            if (TempOutTime == "AM" && TempInTime == "PM")
            {
                diff = (Shiftouthr + 12) - (Shiftinhr - 12);
                Shiftouthr = Shiftouthr + 12;
                Shiftinhr = Shiftinhr - 12;
            }
            else if (TempOutTime == "PM" && TempInTime == "AM")
                diff = Shiftouthr - Shiftinhr;
            else if (TempOutTime == "AM" && TempInTime == "AM")
                diff = Shiftouthr - Shiftinhr;
            else if (TempOutTime == "PM" && TempInTime == "PM")
                diff = Shiftouthr - Shiftinhr;
            if (diff > 0)
                totalSHHours = ((Shiftouthr * 60) + shiftoutmin) - ((Shiftinhr * 60) + (shiftinmin));
            else if (diff == 0)
                totalSHHours = shiftoutmin - shiftinmin;
        }
    }
    private void FindTotalhours(bool ckbStaightShift, int breakhrsMins)
    {
        if (!ckbStaightShift)
        {
            if (totalFHHours != 0 && totalSHHours != 0)
            {
                totalHorus = totalFHHours + totalSHHours;
            }
        }
        else
            totalFHHours = totalFHHours - breakhrsMins;
    }
    private void FindTotalBreakTime()
    {
        if (BStartTime1 != null && BEndTime1 != null && BStartTime2 != null && BEndTime2 != null && BStartTime3 != null && BEndTime3 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime1, BEndTime1);
            CheckBreakTimeSecondHalf(BStartTime1, BEndTime1);
            CheckBreakTimeFirstHalf(BStartTime2, BEndTime2);
            CheckBreakTimeSecondHalf(BStartTime2, BEndTime2);
            CheckBreakTimeFirstHalf(BStartTime3, BEndTime3);
            CheckBreakTimeSecondHalf(BStartTime3, BEndTime3);
        }
        else if (BStartTime1 != null && BEndTime1 != null && BStartTime2 != null && BEndTime2 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime1, BEndTime1);
            CheckBreakTimeSecondHalf(BStartTime1, BEndTime1);
            CheckBreakTimeFirstHalf(BStartTime2, BEndTime2);
            CheckBreakTimeSecondHalf(BStartTime2, BEndTime2);
        }
        else if (BStartTime1 != null && BEndTime1 != null && BStartTime3 != null && BEndTime3 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime1, BEndTime1);
            CheckBreakTimeSecondHalf(BStartTime1, BEndTime1);
            CheckBreakTimeFirstHalf(BStartTime3, BEndTime3);
            CheckBreakTimeSecondHalf(BStartTime3, BEndTime3);
        }
        else if (BStartTime2 != null && BEndTime2 != null && BStartTime3 != null && BEndTime3 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime2, BEndTime2);
            CheckBreakTimeSecondHalf(BStartTime2, BEndTime2);
            CheckBreakTimeFirstHalf(BStartTime3, BEndTime3);
            CheckBreakTimeSecondHalf(BStartTime3, BEndTime3);
        }
        else if (BStartTime1 != null && BEndTime1 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime1, BEndTime1);
            CheckBreakTimeSecondHalf(BStartTime1, BEndTime1);
        }
        //for the Second Break Time Calculation added by Prabakaran.p
        else if (BStartTime2 != null && BEndTime2 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime2, BEndTime2);
            CheckBreakTimeSecondHalf(BStartTime2, BEndTime2);
        }
        //for the Third Break Time Calculation added by Prabakaran.p
        else if (BStartTime3 != null && BEndTime3 != null)
        {
            CheckBreakTimeFirstHalf(BStartTime3, BEndTime3);
            CheckBreakTimeSecondHalf(BStartTime3, BEndTime3);
        }
        else
        {
            TotalBreakHrs = 0;
        }
    }
    /// <summary>
    /// Added by Manjula.P on 30-Sep-2013 for checking break in and out time.
    /// </summary>
    public void CheckBreakAvailable()
    {
        if ((BStartTime1 != null && BEndTime1 != null) || (BStartTime2 != null && BEndTime2 != null) || (BStartTime3 != null && BEndTime3 != null))
        {
            int intActualBreakAvailable = 0;
            DateTime dtFHEndTime = DateTime.Parse(FHEnd);
            DateTime dtSHStartTime = DateTime.Parse(SHStart);
            TimeSpan ts = dtSHStartTime.Subtract(dtFHEndTime);
            if (ts.TotalMinutes <= 0)
                InvalidBreakTimeErrorMsg();
        }
    }
    private void CheckBreakTimeFirstHalf(string strBIn, string strBOut)
    {
        int intTotalBreakHrs = 0;
        string strFHEnd = string.Empty;
        string strBrEnd = string.Empty;
        DateTime dtFHStartTime = Convert.ToDateTime(FHStart);
        //Add End Time date with 1 day for PM and AM Changes
        strFHEnd = FindEndTimeDate(FHStart, FHEnd);
        DateTime dtFHEndTime = Convert.ToDateTime(strFHEnd);
        DateTime dtBIn = Convert.ToDateTime(strBIn);
        //Add End Time date with 1 day for PM and AM Changes
        strBrEnd = FindEndTimeDate(strBIn, strBOut);
        DateTime dtBOut = Convert.ToDateTime(strBrEnd);
        TimeSpan ts = dtBOut.Subtract(dtBIn);
        if (ts.TotalMinutes > 0)
        {
            intTotalBreakHrs = (((ts.Hours) * 60) + (ts.Minutes));
            if (intTotalBreakHrs >= 0)
            {
                //Checks Whether Break In and Out time falls between Shift In and  Shift out time        
                if (FHStart.Substring(6) == "PM" && FHEnd.Substring(6) == "AM")
                {
                    //Case1
                    if (strBIn.Substring(6) == "AM" && strBOut.Substring(6) == "AM")
                    {
                        dtBIn = dtBIn.AddDays(1);
                        dtBOut = dtBOut.AddDays(1);
                        if ((dtFHEndTime > dtBIn) && (dtFHEndTime <= dtBOut))
                            InvalidBreakTimeErrorMsg();
                        if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                        {
                            //InvalidBreakTimeErrorMsg();
                        }
                        else
                            totalFHHours = totalFHHours - intTotalBreakHrs;
                    }
                    else if (strBIn.Substring(6) == "PM" && strBOut.Substring(6) == "AM")
                    {
                        if ((dtFHEndTime > dtBIn) && (dtFHEndTime <= dtBOut))
                            InvalidBreakTimeErrorMsg();
                        if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                        {
                            //InvalidBreakTimeErrorMsg();
                        }
                        else
                            totalFHHours = totalFHHours - intTotalBreakHrs;
                    }
                    else
                    {
                        if ((dtFHEndTime > dtBIn) && (dtFHEndTime <= dtBOut))
                            InvalidBreakTimeErrorMsg();
                        if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                        {
                            //InvalidBreakTimeErrorMsg();
                        }
                        else
                            totalFHHours = totalFHHours - intTotalBreakHrs;
                    }
                }
                else
                {
                    if ((dtFHEndTime > dtBIn) && (dtFHEndTime <= dtBOut))
                    {
                        InvalidBreakTimeErrorMsg();
                    }
                    ///Checking BreakIn time1 and Break outTime 1
                    if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                    {
                        //Uncommented by Manjula.P on 30-Sep-2013 for getting Invalid break time for 01:01 to 01:31 case without break time availbale. Mantis : 0018989 
                        //InvalidBreakTimeErrorMsg();
                    }
                    else
                    {
                        totalFHHours = totalFHHours - intTotalBreakHrs;
                    }
                }
            }
            else
            {
                InvalidBreakTimeErrorMsg();
            }
        }
        else
        {
            InvalidBreakTimeErrorMsg();
        }
    }
    private void CheckBreakTimeSecondHalf(string strBIn, string strBOut)
    {
        int intTotalBreakHrs = 0;
        string strSHEnd = string.Empty;
        string strBrEnd = string.Empty;
        DateTime dtFHStartTime = Convert.ToDateTime(SHStart);
        //Add End Time date with 1 day for PM and AM Changes
        strSHEnd = FindEndTimeDate(SHStart, SHEnd);
        DateTime dtFHEndTime = Convert.ToDateTime(strSHEnd);
        DateTime dtBIn = Convert.ToDateTime(strBIn);
        //Add End Time date with 1 day for PM and AM Changes
        strBrEnd = FindEndTimeDate(strBIn, strBOut);
        DateTime dtBOut = Convert.ToDateTime(strBrEnd);
        TimeSpan ts = dtBOut.Subtract(dtBIn);
        if (ts.TotalMinutes > 0)
        {
            intTotalBreakHrs = (((ts.Hours) * 60) + (ts.Minutes));
            if (intTotalBreakHrs >= 0)
            {
                //Checks Whether Break In and Out time falls between Shift In and  Shift out time        
                if (SHStart.Substring(6) == "PM" && SHEnd.Substring(6) == "AM")
                {
                    //Case1
                    if (strBIn.Substring(6) == "AM" && strBOut.Substring(6) == "AM")
                    {
                        dtBIn = dtBIn.AddDays(1);
                        dtBOut = dtBOut.AddDays(1);
                        if ((dtFHStartTime >= dtBIn) && (dtFHStartTime < dtBOut))
                        {
                            InvalidBreakTimeErrorMsg();
                        }
                        if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                        {
                            //InvalidBreakTimeErrorMsg();
                        }
                        else
                        {
                            totalSHHours = totalSHHours - intTotalBreakHrs;
                        }
                    }
                    else if (strBIn.Substring(6) == "PM" && strBOut.Substring(6) == "AM")
                    {
                        if ((dtFHStartTime >= dtBIn) && (dtFHStartTime < dtBOut))
                        {
                            InvalidBreakTimeErrorMsg();
                        }
                        if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                        {
                            //InvalidBreakTimeErrorMsg();
                        }
                        else
                        {
                            totalSHHours = totalSHHours - intTotalBreakHrs;
                        }
                    }
                    else
                    {
                        if ((dtFHStartTime >= dtBIn) && (dtFHStartTime < dtBOut))
                        {
                            InvalidBreakTimeErrorMsg();
                        }
                        if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                        {
                            //InvalidBreakTimeErrorMsg();
                        }
                        else
                        {
                            totalSHHours = totalSHHours - intTotalBreakHrs;
                        }
                    }
                }
                else
                {
                    if ((dtFHStartTime >= dtBIn) && (dtFHStartTime < dtBOut))
                    {
                        InvalidBreakTimeErrorMsg();
                    }
                    ///Checking BreakIn time1 and Break outTime 1
                    if (!((dtBIn > dtFHStartTime) && (dtBIn < dtFHEndTime) && (dtBOut > dtFHStartTime) && (dtBOut < dtFHEndTime)))
                    {
                        //InvalidBreakTimeErrorMsg();
                    }
                    else
                    {
                        totalSHHours = totalSHHours - intTotalBreakHrs;
                    }
                }
            }
            else
            {
                InvalidBreakTimeErrorMsg();
            }
        }
        else
        {
            InvalidBreakTimeErrorMsg();
        }
    }
    private void InvalidBreakTimeErrorMsg()
    {
        Response.Clear();
        Response.Write("InvalidBreakTime");
        Response.End();
    }
    private void FindWorkingHours()
    {
        string result = "", result1 = "", result2 = "", result3 = "";
        if (totalFHHours != 0)
        {
            int FHWorkHrs = totalFHHours;
            int fhrs = (FHWorkHrs / 60);
            int fmin = (FHWorkHrs % 60);
            string firsth;
            if (fhrs >= 10)
                firsth = fhrs.ToString();
            else
                firsth = "0" + fhrs.ToString();
            string firstm;
            if (fmin >= 10)
                firstm = ":" + fmin.ToString();
            else
                firstm = ":0" + fmin.ToString();
            result1 = firsth + firstm;
        }
        if (totalSHHours != 0)
        {
            int SHWorkHrs = totalSHHours;
            int shrs = (SHWorkHrs / 60);
            int smin = (SHWorkHrs % 60);
            string secondh;
            if (shrs >= 10)
                secondh = shrs.ToString();
            else
                secondh = "0" + shrs.ToString();
            string secondm;
            if (smin >= 10)
                secondm = ":" + smin.ToString();
            else
                secondm = ":0" + smin.ToString();
            result2 = secondh + secondm;
        }
        int WorkingHrs = (totalFHHours) + (totalSHHours);
        int hrs = (WorkingHrs / 60);
        int min = (WorkingHrs % 60);
        if (hrs != 24)
        {
            string th;
            if (hrs >= 10)
                th = hrs.ToString();
            else
                th = "0" + hrs.ToString();
            string tm;
            if (min >= 10)
                tm = ":" + min.ToString();
            else
                tm = ":0" + min.ToString();
            result3 = th + tm;
            Response.Clear();
            result = result1 + "*" + result2 + "*" + result3;
            Response.Write(result);
            Response.End();
        }
        else
        {
            Response.Clear();
            Response.Write("InvalidShiftTime");
            Response.End();
        }
    }
    private string FindEndTimeDate(string StartTime, string EndTime)
    {
        TempOutTime = EndTime.Substring(6, 2);
        TempInTime = StartTime.Substring(6, 2);
        if (TempOutTime == "AM" && TempInTime == "PM")
            EndTime = DateTime.Parse(EndTime).AddDays(1).ToString();
        return EndTime;
    }
}
