/*

   Frontend page: Curtailment Page
   Title: Searching curtailment details on curtailment list
   Designed: Irfan Mam
   User story: 
   Developed: Irfan MAM
   Date created: 3/18/2016

*/ 
        public ActionResult SearchCurtailment(string identificationNumber, string year, string make, string vehicleModel , CurtailmentScheduleModel CurtailmentList)
        {
            List<CurtailmentShedule> SearchList = new List<CurtailmentShedule>();
            
            // if atleast one arg has the value, do search
            if (((!string.IsNullOrEmpty(identificationNumber)) || (!string.IsNullOrEmpty(year)) || (!string.IsNullOrEmpty(make)) || (!string.IsNullOrEmpty(vehicleModel))))
            {
                
                Search sc = new Search();  // search object

                //search through list of elements
                SearchList = sc.GetSearchCurtailmentList(CurtailmentList, identificationNumber.Trim().ToLower(), year.Trim().ToLower(), make.Trim().ToLower(), vehicleModel.Trim().ToLower() );


                // return the partial page of searched elements
                return PartialView(SearchList);
            }
            else
            {
                // return the partial page of empty elements
                return PartialView(SearchList);
            }
        }
        public List<CurtailmentShedule> GetSearchCurtailmentList(CurtailmentScheduleModel curtailmentList, string identificationNumber, string year, string make, string vehicleModel )
        {

            List<CurtailmentShedule> searchList = new List<CurtailmentShedule>();
            //traverse through each element in list
            foreach ( CurtailmentShedule curtailmentShedule in curtailmentList.CurtailmentScheduleInfoModel)
            {
                //search by identification number,year,make,model
                if (!string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && curtailmentShedule.Year.ToString().ToLower().StartsWith(year) && curtailmentShedule.Make.ToLower().StartsWith(make) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by identification number,year,make
                else if (!string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && curtailmentShedule.Year.ToString().ToLower().StartsWith(year) && curtailmentShedule.Make.ToLower().StartsWith(make))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by identification number,year
                else if (!string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && curtailmentShedule.Year.ToString().ToLower().StartsWith(year))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by identification number
                else if (!string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by year,make,model
                else if (string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.Year.ToString().ToLower().StartsWith(year) && curtailmentShedule.Make.ToLower().StartsWith(make) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by make,model
                else if (string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.Make.ToLower().StartsWith(make) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by model
                else if (string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (!string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by year
                else if (string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {

                    if (curtailmentShedule.Year.ToString().ToLower().StartsWith(year))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by make
                else if (string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.Make.ToLower().StartsWith(make))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by year,make
                else if (string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.Year.ToString().ToLower().StartsWith(year) && curtailmentShedule.Make.ToLower().StartsWith(make))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by identification number,model
                else if (!string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by identification number,make
                else if (!string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && curtailmentShedule.Make.ToLower().StartsWith(make))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
              
                //search by identification number,make,model
                else if (!string.IsNullOrEmpty(identificationNumber) && string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && curtailmentShedule.Make.ToLower().StartsWith(make) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by identification number,year,model
                else if (!string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.IDNumber.ToLower().EndsWith(identificationNumber) && curtailmentShedule.Year.ToString().ToLower().StartsWith(year) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
                //search by year,model
                else if (string.IsNullOrEmpty(identificationNumber) && !string.IsNullOrEmpty(year) && string.IsNullOrEmpty(make) && !string.IsNullOrEmpty(vehicleModel))
                {
                    if (curtailmentShedule.Year.ToString().ToLower().StartsWith(year) && !string.IsNullOrEmpty(curtailmentShedule.Model) && curtailmentShedule.Model.ToLower().StartsWith(vehicleModel))
                    {
                        searchList.Add(curtailmentShedule);
                    }
                }
            }
            return searchList;
        }
        /*

 Frontend page: Curtailment Page
 Title: Curtailments by selected due date
 Designed: Nadeeka
 User story: 
 Developed: Nadeeka
 Date created: 3/21/2016

*/
        public ActionResult PayCurtailmentForSelectedDueDate(DateTime dueDate)
        {
            CurtailmentScheduleModel curtailmentScheduleModel = new CurtailmentScheduleModel();
            LoanSetupStep1 loanDetails = new LoanSetupStep1();
            loanDetails = (new LoanSetupAccess()).GetLoanDetailsByLoanCode(Session["loanCode"].ToString()); // take loan details of selected loan
            ViewBag.loanDetails = loanDetails;

            string f = dueDate.ToShortDateString();
            DateTime dd = Convert.ToDateTime(f);


            CurtailmentAccess curtailmentAccess = new CurtailmentAccess();
            List<CurtailmentShedule> curtailmentSchedule = curtailmentAccess.GetCurtailmentScheduleByDueDate(loanDetails.loanId, dd);  // get curtailment list by due date
            ViewBag.DealerEmail = (string)Session["DealerEmail"]; // pass dealer email to the view
            ViewBag.LoanId = loanDetails.loanId;  // pass loan id to the view

            curtailmentScheduleModel.CurtailmentScheduleInfoModel = new List<CurtailmentShedule>();
            curtailmentScheduleModel.CurtailmentScheduleInfoModel.AddRange(curtailmentSchedule); // bind the list of curtailment to model
            curtailmentScheduleModel.DueDate = dueDate; // bind the due date to model

            // return the partial view page
            return PartialView(curtailmentScheduleModel);
        }