using FSI.Prj.Mgt.Data;
using FSI.Prj.Mgt.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using X.PagedList;

namespace FSI.Prj.Mgt.Controllers
{
    public class ProjectController : Controller
    {
        private readonly ApplicationDbContext _context;

        public ProjectController(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> Index(string searchString, int? page)
        {

            IQueryable<Project> projects = _context.Projects.Include(x => x.Plant);


            if (!string.IsNullOrEmpty(searchString))
            {
                projects = projects.Where(x => x.No!.ToString().Contains(searchString) || x.Name!.Contains(searchString) || x.Description!.Contains(searchString));

            }

            return View(await projects.ToPagedListAsync(page ?? 1, 10));
        }

        public IActionResult CreateEdit(int id)
        {
            ViewBag.Plants = _context.Plants;

            if (id == 0)
            {
                return View();
            }

            var project = _context.Projects.Find(id);

            if (project == null)
            {
                return NotFound();
            }

            return View(project);
        }

        [HttpPost]
        public IActionResult CreateEditProject(Project project)
        {
            //for (int i = 0; i <= 100000; i++)
            //{
            //    project = new Project()
            //    {
            //        Name = i.ToString(),
            //        Description = i.ToString(),
            //    };

            if (project.Id == 0)
            {
                var highestPrjNo = 0;
                int prjYear = 0;
                int prjNo = 0;

                if (_context.Projects.Count() > 0)
                {
                    highestPrjNo = _context.Projects.Max(x => x.No);
                    prjYear = highestPrjNo;

                    // Projekt Jahr freistellen
                    while (prjYear >= 100)
                        prjYear /= 10;

                    // Laufende Nr. des Jahres freistellen
                    prjNo = highestPrjNo >= 1000 ? highestPrjNo % 1000 : highestPrjNo;
                }

                // Neue Projekt Nummer berechnen
                if (prjYear == int.Parse(DateTime.Now.ToString("yy")))
                {
                    project.No = prjYear * 1000 + prjNo + 1;
                }
                else
                {
                    project.No = int.Parse(DateTime.Now.ToString("yy")) * 1000 + 1;
                }

                _context.Projects.Add(project);
            }
            else
            {
                _context.Projects.Update(project);

            }
            //}

            _context.SaveChanges();

            return RedirectToAction("Index");
        }

        public IActionResult DeleteProject(int id)
        {
            var project = _context.Projects.Find(id);

            if (project == null)
            {
                return NotFound();
            }

            _context.Remove(project);
            _context.SaveChanges();

            return RedirectToAction("Index");
        }
    }
}
