//
 // GET: /DriverDetail/Details/5
 public ViewResult Details(int id)
 {
     //DriverDetail driverdetail = db.DriverDetails.Find(id);
     //return View(driverdetail);
     DriverDetailDAL dal = new DriverDetailDAL();
     DriverDetail driverdetail = dal.DriverDetailFindById(id);
     return View(driverdetail);
 }
        /// <method>
        /// SetDriverDisqualifyStatus() sets status of drivers licence to disqualified
        /// </method>
        public void SetDriverDisqualifyStatus(int id)
        {
            //offences cannot be deleted from the database
            //as they are referenced in the drivingoffence table.
            //Instead an offence will have its status=false
            //so it cannot be used in new driving offences.
            //This ensures integrity within existing driving offences.
            DriverDetailDAL driverDAL = new DriverDetailDAL();
            DriverDetail _driver = driverDAL.DriverDetailFindById(id);

            _driver.DdLicenceStatus= "Disqualified";
            driverDAL.EditDriverDetail(_driver);
        }
        // POST: /ListedOffence/Edit/5
        public void EditDriverOffence(DriverOffenceUI driverOffenceUI)
        {
            DriverOffence _driverOffenceDB = _DAL.DriverOffenceFindById(driverOffenceUI.id);

            //Update offence details
             _driverOffenceDB.doStatus = driverOffenceUI.status.ToString();
             _driverOffenceDB.doLocation = driverOffenceUI.location.ToString();
             _driverOffenceDB.doGardaId = driverOffenceUI.gardaId.ToString();
             _driverOffenceDB.doOffenceDate = driverOffenceUI.offenceDate;

             //Update driver details
             DriverDetailDAL driverDAL = new DriverDetailDAL();
             DriverDetail driver = driverDAL.DriverDetailFindById(_driverOffenceDB.DriverDetail.Id);

             _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();
            //store updated driver details in database
            driverDAL.EditDriverDetail(_driverOffenceDB.DriverDetail);

            //update vehicle details
             VehicleDetailDAL vehicleDAL = new VehicleDetailDAL();
             VehicleDetail vehicle = vehicleDAL.DeleteVehicleDetailById(_driverOffenceDB.VehicleDetails.Id);

            _driverOffenceDB.VehicleDetails.VdRegistration = driverOffenceUI.Registration.ToString();
            _driverOffenceDB.VehicleDetails.VdMake = driverOffenceUI.Make.ToString();
            _driverOffenceDB.VehicleDetails.VdType = driverOffenceUI.Type.ToString();
            _driverOffenceDB.VehicleDetails.VdCubicCapacity = driverOffenceUI.Capacity;
            vehicleDAL.EditVehicleDetail(_driverOffenceDB.VehicleDetails);

            _DAL.EditDriverOffence(_driverOffenceDB);
        }
        /// <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);
        }