//Should return identical to time card lines
        public HttpResponseMessage<PunchResponse> GetTimeCardDetails(string empId, int tcId = -1)
        {
            
            if (tcId == -1)
            {
                var emp = db.Employees.SingleOrDefault(e => e.EmployeeID == empId);
                var payPeriod = PayPeriodTools.LookupPayPeriod(db, emp.DepartmentID);
                var tcLookup = emp.Timecards.SingleOrDefault(tc => tc.PayPeriod == payPeriod.Start);
                tcId = tcLookup.TimecardID;
            }
                
            Timecard curTimeCard = db.Timecards.SingleOrDefault(tc => tc.TimecardID == tcId);

            var timeCardData = db.Lines.Where(l => l.TimecardID == curTimeCard.TimecardID).OrderBy(l => l.SplitStart).ToList();

            var lines = TimeCardView.LinesToTimeCardView(timeCardData);

            PunchResponse ret = new PunchResponse() { lines = lines };

            return new HttpResponseMessage<PunchResponse>(ret);
            
        }
        public HttpResponseMessage<PunchResponse> Punch(PunchRequest request)
        {
 
            Employee emp = db.Employees.FirstOrDefault(e => e.EmployeeID.Equals(request.ID));

            if (!emp.Pin.Equals(request.pin)) // the pin didn't match don't do anything
            {
                PunchResponse response = new PunchResponse()
                {
                    isSuccess = false,
                    pinError = "Pin and user did not match.",
                    lines = null,
                    generalError = null
                };

                return new HttpResponseMessage<PunchResponse>(response);
            }
            else
            {
                var payPeriod = PayPeriodTools.LookupPayPeriod(db, emp.DepartmentID);
                var curTimeCard = emp.Timecards.SingleOrDefault(tc => tc.PayPeriod == payPeriod.Start);

                PunchResponse retVal = new PunchResponse()
                {
                    isSuccess = true,
                    pinError = "",
                    generalError = null
                };

                if (request.closesPunch == -1)  // Create a new punch in the DB
                {
                    Punch temp = new Punch()
                    {
                        EmployeeID = emp.EmployeeID,
                        InTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0),
                        OutTime = null,
                        DepartmentID = emp.DepartmentID,
                        PunchTypeID = Constants.DEFAULT_PUNCH_TYPE // Make this equal to the Regular punch type.
                    };

                    db.Punches.Add(temp);
                    db.SaveChanges();

                    var timeCardData = TimeCardView.LinesToTimeCardView(db.Lines.Where(l => l.TimecardID == curTimeCard.TimecardID).OrderBy(l => l.SplitStart));
                    // We add the last line to just display the information, letting the user know that we register the punch in
                    timeCardData.Add(new TimeCardView()
                    {
                        DateText = DateTime.Now.ToString(@"MM\/dd\/yy"),
                        InText = temp.InTime.ToString(@"hh\:mm"),
                        OutText = "",
                        RegularText = "",
                        OvertimeText = "",
                        DoubletimeText = ""
                    });

                    retVal.lines = timeCardData;
                }
                else // We need to close the last punch and make lines -- Make it split the lines over the payperiod seed
                {
                    // Set the ending time of the punch
                    Punch currentPunch = db.Punches.First(p => p.PunchID == request.closesPunch);
                    currentPunch.OutTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);

                    var timeCardData = db.Lines.Where(l => l.TimecardID == curTimeCard.TimecardID).OrderBy(l => l.SplitStart).ToList();

                    retVal.lines = TimeCardView.LinesToTimeCardView(timeCardData);

                    if (currentPunch.OutTime.Value.Subtract(currentPunch.InTime).TotalMinutes < 1) // punch was shorter than a minut.
                    {
                        db.Punches.Remove(currentPunch);
                        retVal.lines.Add(new TimeCardView() // Make the last line show, but mark is as rapid since it won't get put in the DB
                        {
                            DateText = currentPunch.InTime.ToString(@"MM\/dd\/yy"),
                            InText = currentPunch.InTime.ToString(@"hh\:mm"),
                            OutText = currentPunch.OutTime.Value.ToString(@"hh\:mm"),
                            RegularText = "00:00",
                            OvertimeText = "00:00",
                            DoubletimeText = "00:00",
                            isRapid = true
                        });
                    }
                    else // Punch was longer than a minut, we add it to the DB.
                    {
                        Calculations.addLines(db, currentPunch);
                    }

                    db.SaveChanges();
                }

                return new HttpResponseMessage<PunchResponse>(retVal);


            }
       }