using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Bilfinger.HRcules.SuccessFactorData.Model;


namespace Bilfinger.HRcules.SuccessFactorWebFrontend.Controllers
{
    public class PeopleController : Controller
    {

        #region Personenpflege
        public ActionResult Index()
        {
            var personChooseCompanyViewModel = new PersonChooseCompanyViewModel();
            List<CimOrganisation> organisations = new List<CimOrganisation>();
            organisations.Add(new CimOrganisation("01", "Firma 01"));
            organisations.Add(new CimOrganisation("02", "Firma 02"));
            personChooseCompanyViewModel.CimOrganizationsForUser = organisations.AsQueryable();


            return View(personChooseCompanyViewModel);
        }

        public ActionResult PeopleSuccessFactor_Read([DataSourceRequest]DataSourceRequest request, string cimId)
        {
            List<PersonSuccessFactor> people = new List<PersonSuccessFactor>();
            people.Add(new PersonSuccessFactor { Id = 1, FirstName = "Firstname 1", LastName = "LastName 1 Company " + cimId, LegalEntity = cimId });
            people.Add(new PersonSuccessFactor { Id = 2, FirstName = "Firstname 2", LastName = "LastName 2 Company " + cimId, LegalEntity = cimId });
            people.Add(new PersonSuccessFactor { Id = 3, FirstName = "Firstname 3", LastName = "LastName 3 Company " + cimId, LegalEntity = cimId });
            people.Add(new PersonSuccessFactor { Id = 4, FirstName = "Firstname 4", LastName = "LastName 4 Company " + cimId, LegalEntity = cimId });
            if (cimId.Equals("02"))
            {
                people.Add(new PersonSuccessFactor { Id = 5, FirstName = "Firstname 5", LastName = "LastName 5 Company " + cimId, LegalEntity = cimId });
                people.Add(new PersonSuccessFactor { Id = 6, FirstName = "Firstname 6", LastName = "LastName 6 Company " + cimId, LegalEntity = cimId });
                people.Add(new PersonSuccessFactor { Id = 7, FirstName = "Firstname 7", LastName = "LastName 7 Company " + cimId, LegalEntity = cimId });
            }
            IQueryable<PersonSuccessFactor> peoplesuccessfactor = people.AsQueryable();
            DataSourceResult result = peoplesuccessfactor.ToDataSourceResult(request, c => new PersonSuccessFactorViewModel
            {
                Id = c.Id,
                UserId = c.UserId,
                UserName = c.UserName,
                FirstName = c.FirstName,
                LastName = c.LastName,
                Function = c.Function,
                Email = c.Email,
                Manager_Id = c.Manager_Id,
                Manager = c.Manager == null ? null : new PersonSelectViewModel { Id = c.Manager.Id, Name = string.Format("{0}, {1} ({2})", c.Manager.LastName, c.Manager.FirstName, c.Manager.UserName) },
                Department = c.Department,
                BusinessArea = c.BusinessArea,
                Location = c.Location,
                Gender = c.Gender,
                HrResponsible_Id = c.HrResponsible_Id,
                HireDate = c.HireDate,
                Country = c.Country,
                BusinessSegment = c.BusinessSegment,
                BusinessUnit = c.BusinessUnit,
                CostCenter = c.CostCenter,
                ManagementLevel = c.ManagementLevel,
                Mitarbeiterkreis = c.Mitarbeiterkreis,
                CostCenterResponsible_Id = c.CostCenterResponsible_Id,
                ExposedFunctions = c.ExposedFunctions,
                JobFamily = c.JobFamily,
                LoginMethod = c.LoginMethod,
                DefaultLocale = c.DefaultLocale,
                PcArbeitsplatz = c.PcArbeitsplatz
            });

            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult PeopleSuccessFactor_Create([DataSourceRequest]DataSourceRequest request, PersonSuccessFactorViewModel personSuccessFactor, string cimId)
        {
            if (ModelState.IsValid)
            {
                var entity = new PersonSuccessFactor
                {
                    UserId = personSuccessFactor.UserId,
                    UserName = personSuccessFactor.UserName,
                    FirstName = personSuccessFactor.FirstName,
                    LastName = personSuccessFactor.LastName,
                    Function = personSuccessFactor.Function,
                    Email = personSuccessFactor.Email,
                    Manager_Id = personSuccessFactor.GetManagerId,
                    Department = personSuccessFactor.Department,
                    BusinessArea = personSuccessFactor.BusinessArea,
                    Location = personSuccessFactor.Location,
                    Gender = personSuccessFactor.Gender,
                    HrResponsible_Id = personSuccessFactor.GetHrResponsibleId,
                    HireDate = personSuccessFactor.HireDate,
                    Country = personSuccessFactor.Country,
                    BusinessSegment = personSuccessFactor.BusinessSegment,
                    LegalEntity = cimId,
                    BusinessUnit = personSuccessFactor.BusinessUnit,
                    CostCenter = personSuccessFactor.CostCenter,
                    ManagementLevel = personSuccessFactor.ManagementLevel,
                    Mitarbeiterkreis = personSuccessFactor.Mitarbeiterkreis,
                    CostCenterResponsible_Id = personSuccessFactor.GetCostCenterResponsibleId,
                    ExposedFunctions = personSuccessFactor.ExposedFunctions,
                    JobFamily = personSuccessFactor.JobFamily,
                    LoginMethod = personSuccessFactor.LoginMethod,
                    DefaultLocale = personSuccessFactor.DefaultLocale,
                    PcArbeitsplatz = personSuccessFactor.PcArbeitsplatz
                };
                personSuccessFactor.Id = entity.Id;
            }

            return Json(new[] { personSuccessFactor }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult PeopleSuccessFactor_Update([DataSourceRequest]DataSourceRequest request, PersonSuccessFactorViewModel personSuccessFactor, string cimId)
        {
            if (ModelState.IsValid)
            {
                var entity = new PersonSuccessFactor
                {
                    Id = personSuccessFactor.Id,
                    UserId = personSuccessFactor.UserId,
                    UserName = personSuccessFactor.UserName,
                    FirstName = personSuccessFactor.FirstName,
                    LastName = personSuccessFactor.LastName,
                    Function = personSuccessFactor.Function,
                    Email = personSuccessFactor.Email,
                    Manager_Id = personSuccessFactor.GetManagerId,
                    Department = personSuccessFactor.Department,
                    BusinessArea = personSuccessFactor.BusinessArea,
                    Location = personSuccessFactor.Location,
                    Gender = personSuccessFactor.Gender,
                    HrResponsible_Id = personSuccessFactor.GetHrResponsibleId,
                    HireDate = personSuccessFactor.HireDate,
                    Country = personSuccessFactor.Country,
                    BusinessSegment = personSuccessFactor.BusinessSegment,
                    LegalEntity = cimId,
                    BusinessUnit = personSuccessFactor.BusinessUnit,
                    CostCenter = personSuccessFactor.CostCenter,
                    ManagementLevel = personSuccessFactor.ManagementLevel,
                    Mitarbeiterkreis = personSuccessFactor.Mitarbeiterkreis,
                    CostCenterResponsible_Id = personSuccessFactor.GetCostCenterResponsibleId,
                    ExposedFunctions = personSuccessFactor.ExposedFunctions,
                    JobFamily = personSuccessFactor.JobFamily,
                    LoginMethod = personSuccessFactor.LoginMethod,
                    DefaultLocale = personSuccessFactor.DefaultLocale,
                    PcArbeitsplatz = personSuccessFactor.PcArbeitsplatz
                };

            }

            return Json(new[] { personSuccessFactor }.ToDataSourceResult(request, ModelState));
        }
        #endregion



        [HttpPost]
        public ActionResult Excel_Export_Save(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}
