/// <method>
        /// StatisticsDriver() returns 'Penalty Points Incurred Per Driver Per County' statistics.
        /// </method>
        public string StatisticsDriver()
        {
            //Get list of drivers
            //Get list of driving offences.
            //For each driver store total penalty points and county of residence.
            //For each county store total drivers per penalty points
            //Return results.
            DriverDetailDAL driverDAL = new DriverDetailDAL();
            List<DriverDetail> allDrivers = driverDAL.ListOfDriverDetails();
            List<DriverOffence> _drivingOffenceDB = _DAL.ListOfDriverOffences();
            DriverPointsUI newDriver = new DriverPointsUI();
            List<DriverPointsUI> _uniqueDrivers = new List<DriverPointsUI>();
            List<string> countyNames = new List<string>();
            List<DriverPointsByCountyUI> _totalPointsByCounty = new List<DriverPointsByCountyUI>();
            DriverPointsByCountyUI pointsByCounty = new DriverPointsByCountyUI();

            bool driverExists = false;
            foreach (DriverDetail driver in allDrivers)
            {
               foreach (DriverOffence offence in _drivingOffenceDB) //  for each file
                {
                    if (offence.DriverDetail.DdLicenceNo == driver.DdLicenceNo)
                    {
                        //This is another driving offence belonging to this driver.
                        //Store the drivers county of residence.
                        //Store the drivers licence Number.
                        //Check penalty points belonging to offence
                        //Add the penalty points to the drivers total
                        //Store the driver information
                        newDriver.County = offence.DriverDetail.DdAddress3.ToString();
                        if (offence.doStatus == "New Offence")
                            //penalty points not applied yet.
                            newDriver.AccumulatedPoints = newDriver.AccumulatedPoints + 0;
                        else if ((offence.doStatus == "Penalty Notification") ||
                            (offence.doStatus == "Penalty 28 Days Notification") ||
                            (offence.doStatus == "Court Summons"))
                        {
                            //apply penalty points on conviction
                            newDriver.AccumulatedPoints = newDriver.AccumulatedPoints + offence.ListedOffence.Lo56days;
                            driverExists = true;
                        }
                        else if (offence.doStatus == "Fine Paid")
                        {
                            //apply penalty points on payment
                            newDriver.AccumulatedPoints = newDriver.AccumulatedPoints + offence.ListedOffence.Lo28Days;
                            driverExists = true;
                        }

                    }//end if
                }//end for _drivingOffenceDB

               if (driverExists == true)
               {
                   //store driver information
                   _uniqueDrivers.Add(newDriver);
                   //Reset for next driver
                   driverExists = false;
                   newDriver.County ="";
                   newDriver.AccumulatedPoints = 0;
               }
            } //end foreach allDrivers

            //Create a list of countys with offenders.
            bool countyExists = false;
            foreach (DriverPointsUI driver in _uniqueDrivers)
            {
                foreach (string county in countyNames)
                {
                    if (driver.County == county)
                    {
                        countyExists = true;
                        break;
                    }
                }
                if (countyExists == false)
                    countyNames.Add(driver.County.ToString());
                else if (countyExists == true)
                    countyExists = false;

            }
            //calculate penalty points per driver per county.
            int driverPoints = 0;
            foreach (string county in countyNames)
            {
                pointsByCounty.name = county;
                foreach (DriverPointsUI driver in _uniqueDrivers)
                {
                    if (driver.County == county)
                    {
                        //Driver resides in this county
                        //Add driver to driver total
                        //Add the driver to penalty point total.
                        pointsByCounty.TotalDrivers = pointsByCounty.TotalDrivers + 1;
                        if (driver.AccumulatedPoints > 12)
                            driverPoints = 12;
                        else
                            driverPoints = driver.AccumulatedPoints;

                        switch (driverPoints)
                        {
                            case 1:
                                pointsByCounty.TotalP1 = pointsByCounty.TotalP1 + 1;
                                break;
                            case 2:
                                pointsByCounty.TotalP2 = pointsByCounty.TotalP2 + 1;
                                break;
                            case 3:
                                pointsByCounty.TotalP3 = pointsByCounty.TotalP3 + 1;
                                break;
                            case 4:
                                pointsByCounty.TotalP4 = pointsByCounty.TotalP4 + 1;
                                break;
                            case 5:
                                pointsByCounty.TotalP5 = pointsByCounty.TotalP5 + 1;
                                break;
                            case 6:
                                pointsByCounty.TotalP6 = pointsByCounty.TotalP6 + 1;
                                break;
                            case 7:
                                pointsByCounty.TotalP7 = pointsByCounty.TotalP7 + 1;
                                break;
                            case 8:
                                pointsByCounty.TotalP8 = pointsByCounty.TotalP8 + 1;
                                break;
                            case 9:
                                pointsByCounty.TotalP8 = pointsByCounty.TotalP9 + 1;
                                break;
                            case 10:
                                pointsByCounty.TotalP10 = pointsByCounty.TotalP10 + 1;
                                break;
                            case 11:
                                pointsByCounty.TotalP11 = pointsByCounty.TotalP11 + 1;
                                break;
                            case 12:
                                pointsByCounty.TotalP12 = pointsByCounty.TotalP12 + 1;
                                break;
                            default:
                                break;
                        }//end switch
                        driverPoints = 0;  //reset for next driver
                    } //end if
                }// end for _uniqueDrivers

                _totalPointsByCounty.Add(pointsByCounty);//store county points by driver
                pointsByCounty.name = "";
                pointsByCounty.TotalDrivers = 0;
                pointsByCounty.TotalP1 = 0;
                pointsByCounty.TotalP2 = 0;
                pointsByCounty.TotalP3 = 0;
                pointsByCounty.TotalP4 = 0;
                pointsByCounty.TotalP5 = 0;
                pointsByCounty.TotalP6 = 0;
                pointsByCounty.TotalP7 = 0;
                pointsByCounty.TotalP8 = 0;
                pointsByCounty.TotalP9 = 0;
                pointsByCounty.TotalP10 = 0;
                pointsByCounty.TotalP11 = 0;
                pointsByCounty.TotalP12 = 0;
            }

            //loop through county and build message
            string message = "";
            foreach (DriverPointsByCountyUI county in _totalPointsByCounty)
            {
                message = message + "County " + county.name + ": " +
                    county.TotalP1 + " drivers with 1 penalty point. " +
                    county.TotalP2 + " drivers with 2 penalty point. " +
                    county.TotalP3 + " drivers with 3 penalty point. " +
                    county.TotalP4 + " drivers with 4 penalty point. " +
                    county.TotalP5 + " drivers with 5 penalty point. " +
                    county.TotalP6 + " drivers with 6 penalty point. " +
                    county.TotalP7 + " drivers with 7 penalty point. " +
                    county.TotalP8 + " drivers with 8 penalty point. " +
                    county.TotalP9 + " drivers with 9 penalty point. " +
                    county.TotalP10 + " drivers with 10 penalty point. " +
                    county.TotalP11 + " drivers with 11 penalty point. " +
                    county.TotalP12 + " drivers with 12 or more penalty point. " +
                    county.TotalDrivers + " drivers in total accumulated penalty points. ";
            }
            return message;
        }
        /// <method>
        /// Insert offenceUI object into database
        /// </method>
        public void CreateOffence(DriverOffenceUI driverOffenceUI)
        {
            // declare a new driver offence object.
            // if driver does not exist declare a new driver.
            // if vehicle does not exists declare a new vehicle.
            // if driver exists, use existing driver object
            // if vehicle exists, use existing vehicle object
            // retrieve existing offence object from list of offences
            // update driver offence object
            // insert the new driver offence object into the database.

            DriverOffence _driverOffenceDB = new DriverOffence();

            //assign driver offence information to new driver offence
            _driverOffenceDB.doStatus = driverOffenceUI.status.ToString();
            _driverOffenceDB.doLocation = driverOffenceUI.location.ToString();
            _driverOffenceDB.doGardaId= driverOffenceUI.gardaId.ToString();
            _driverOffenceDB.doOffenceDate = driverOffenceUI.offenceDate ;

            //Check to see if driver exists in database
            DriverDetailDAL driverDAL = new DriverDetailDAL();
            List<DriverDetail> driversDB = driverDAL.ListOfDriverDetails();

            int driverExists = 0;
            foreach (DriverDetail driver in driversDB)
            {
                if (driver.DdLicenceNo == driverOffenceUI.LicenceNo)
                {
                    driverExists = driver.Id;
                    break;
                }
            }

            //If driver does not exist declare a new driver object
            if (driverExists == 0)
            {
                _driverOffenceDB.DriverDetail = new DriverDetail();
                //update driver attributes
                _driverOffenceDB.DriverDetail.DdLicenceNo = driverOffenceUI.LicenceNo.ToString();
                _driverOffenceDB.DriverDetail.DdFName = driverOffenceUI.FName.ToString();
                _driverOffenceDB.DriverDetail.DdSName = driverOffenceUI.SName.ToString();
                _driverOffenceDB.DriverDetail.DdAddress1 = driverOffenceUI.Address1.ToString();
                _driverOffenceDB.DriverDetail.DdAddress2 = driverOffenceUI.Address2.ToString();
                _driverOffenceDB.DriverDetail.DdAddress3 = driverOffenceUI.Address3.ToString();
                _driverOffenceDB.DriverDetail.DdLicenceStatus = driverOffenceUI.LicenceStatus.ToString();
                _driverOffenceDB.DriverDetail = driverDAL.CreateDriverDetail(_driverOffenceDB.DriverDetail);
            }

            //if driver existed assign driver details to the new driver offence object;
            if (driverExists > 0)
            {
                _driverOffenceDB.DriverDetail = driverDAL.DriverDetailFindById(driverExists);
                //update driver attributes
                _driverOffenceDB.DriverDetail.DdLicenceNo = driverOffenceUI.LicenceNo.ToString();
                _driverOffenceDB.DriverDetail.DdFName = driverOffenceUI.FName.ToString();
                _driverOffenceDB.DriverDetail.DdSName = driverOffenceUI.SName.ToString();
                _driverOffenceDB.DriverDetail.DdAddress1 = driverOffenceUI.Address1.ToString();
                _driverOffenceDB.DriverDetail.DdAddress2 = driverOffenceUI.Address2.ToString();
                _driverOffenceDB.DriverDetail.DdAddress3 = driverOffenceUI.Address3.ToString();
                _driverOffenceDB.DriverDetail.DdLicenceStatus = driverOffenceUI.LicenceStatus.ToString();
                //_driverOffenceDB.DriverDetail = driverDAL.DriverDetailFindById(driverDAL.EditDriverDetail(_driverOffenceDB.DriverDetail));
                driverDAL.EditDriverDetail(_driverOffenceDB.DriverDetail);
            }

            //Check to see if vehicle exists in database
            VehicleDetailDAL vehicleDAL = new VehicleDetailDAL();
            List<VehicleDetail> vehiclesDB = vehicleDAL.ListOfVehicleDetails();

            int vehicleExists = 0;
            foreach (VehicleDetail vehicle in vehiclesDB)
            {
                if (vehicle.VdRegistration == driverOffenceUI.Registration)
                {
                    vehicleExists = vehicle.Id;
                    break;
                }
            }

            //If vehicle does not exist declare a new vehicle object
            if (vehicleExists == 0)
            {
                _driverOffenceDB.VehicleDetails = new VehicleDetail();
                //update vechile attributes
                _driverOffenceDB.VehicleDetails.VdRegistration = driverOffenceUI.Registration.ToString();
                _driverOffenceDB.VehicleDetails.VdMake = driverOffenceUI.Make.ToString();
                _driverOffenceDB.VehicleDetails.VdType = driverOffenceUI.Type.ToString();
                _driverOffenceDB.VehicleDetails.VdCubicCapacity = driverOffenceUI.Capacity;
                _driverOffenceDB.VehicleDetails = vehicleDAL.CreateVehicleDetail(_driverOffenceDB.VehicleDetails);
            }

            //if vehicle existed assign vehicle details to the vehicle offence object;
            if (vehicleExists > 0)
            {
                _driverOffenceDB.VehicleDetails = vehicleDAL.VehicleDetailFindById(vehicleExists);
                //update vechile attributes
                _driverOffenceDB.VehicleDetails.VdRegistration = driverOffenceUI.Registration.ToString();
                _driverOffenceDB.VehicleDetails.VdMake = driverOffenceUI.Make.ToString();
                _driverOffenceDB.VehicleDetails.VdType = driverOffenceUI.Type.ToString();
                _driverOffenceDB.VehicleDetails.VdCubicCapacity = driverOffenceUI.Capacity;
                //_driverOffenceDB.VehicleDetails = vehicleDAL.VehicleDetailFindById( vehicleDAL.EditVehicleDetail(_driverOffenceDB.VehicleDetails));
                vehicleDAL.EditVehicleDetail(_driverOffenceDB.VehicleDetails);
            }

            //assign offence details to the new driver offence object
            ListedOffenceDAL offenceDAL = new ListedOffenceDAL();
            List<ListedOffence> offencesDB = offenceDAL.ListOfListedOffences();

            foreach (ListedOffence offence in offencesDB)
            {
                if ((offence.LoDesc == driverOffenceUI.description) && (offence.LoStatus))
                {
                    _driverOffenceDB.ListedOffence = offenceDAL.ListedOffenceFindById(offence.Id);
                    break;
                }

            }

            //Create a new driver offence
            int driverOffenceID = _DAL.CreateDriverOffence(_driverOffenceDB);
            //_driverOffenceDB = _DAL.DriverOffenceFindById(driverOffenceID);
            //Update driver offence object in database
            //_DAL.EditDriverOffence(_driverOffenceDB);
        }
        /// <method>
        /// DisqualificationNotification()  runs '12 Point Licence Disqualification Notification' Daily Task.
        /// </method>
        public string DisqualificationNotification()
        {
            //Get list of drivers
            //Get list of driver offences
            //For each driver calculate total penalty points accumulated
            //if penalty points are 12 or more then mark driver status as disqualified
            //Return results.

            DriverDetailDAL driverDAL = new DriverDetailDAL();
            List<DriverDetail> allDrivers = driverDAL.ListOfDriverDetails();
            List<DriverOffence> _drivingOffenceDB = _DAL.ListOfDriverOffences();

            int driverPointsAccumulated = 0;
            bool offencesExists = false;
            string message = "Drivers who've accumulated 12 or more penalty points and now have their drivers licence disqualified are: ";
            foreach (DriverDetail driver in allDrivers)
            {
                //if driver is not already disqualified
                if (driver.DdLicenceStatus != "Disqualified")
                {
                    foreach (DriverOffence offence in _drivingOffenceDB) //  for each file
                    {
                        if (offence.doStatus != "Deleted")
                        {
                            if (offence.DriverDetail.DdLicenceNo == driver.DdLicenceNo)
                            {
                                offencesExists = true;
                                //This is another driving offence belonging to this driver.
                                //Check penalty points belonging to offence
                                //Add the penalty points to the drivers total
                                //IF 12 or more points accumulated then disqualify driver

                                if (offence.doStatus == "New Offence")
                                    //penalty points not applied yet.
                                    driverPointsAccumulated = driverPointsAccumulated + 0;
                                else if ((offence.doStatus == "Penalty Notification") ||
                                    (offence.doStatus == "28 Days Notification") ||
                                    (offence.doStatus == "Court Summons"))
                                {
                                    //apply penalty points on conviction
                                    driverPointsAccumulated = driverPointsAccumulated + offence.ListedOffence.Lo56days;
                                }
                                else if (offence.doStatus == "Fine Paid")
                                {
                                    //apply penalty points on payment
                                    driverPointsAccumulated = driverPointsAccumulated + offence.ListedOffence.Lo28Days;
                                }
                            }//end if
                        }//end if
                    }//end for _drivingOffenceDB

                    if (offencesExists == true)
                    {
                        //build up results.
                        if (driverPointsAccumulated >= 12)
                        {
                            message = message + driver.DdFName +
                                     " " + driver.DdSName + " (" +
                                     driver.DdLicenceNo + ") residing at " +
                                     driver.DdAddress1 + ", " +
                                     driver.DdAddress2 + ", " +
                                     driver.DdAddress3 + " has accumulated " +
                                     driverPointsAccumulated + " penalty points. ";

                            //Update driver licence status to 'disqualified'
                            SetDriverDisqualifyStatus(driver.Id);
                        }//end if driverPointsAccumulated

                        //Reset for next driver
                        offencesExists = false;
                        driverPointsAccumulated = 0;
                    } //end if offencesExists
                } //end if driver is not already disqualified
            } //end foreach allDrivers

            return message;
        }
        //
        // GET: /DriverDetail/
        public ViewResult Index()
        {
            //return View(db.DriverDetails.ToList());

            DriverDetailDAL dal = new DriverDetailDAL();
            List<DriverDetail> driverdetail = dal.ListOfDriverDetails();
            return View(driverdetail);
        }