using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;

namespace wispro.sp.api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class CalMonthController : ControllerBase
    {
        spDbContext Context;

        public CalMonthController(spDbContext context)
        {
            Context = context;
        }

        public CalMonth GetHandlingMonth()
        {
            CalMonth calMonth= Context.CalMonths.Where<CalMonth>(c => c.Status != 4).FirstOrDefault();

            if(calMonth== null)
            {
                return null;
            }
            else
            {
                return calMonth;
            }
        }

        public List<CalMonth> GetAll()
        {
            var lstAll= Context.CalMonths.ToList();

            lstAll.Sort((x,y)=> {
                DateTime str1 = DateTime.Parse( $"{x.Year}-{x.Month}-01");
                DateTime str2 = DateTime.Parse($"{y.Year}-{y.Month}-01");

                return str2.CompareTo(str1);
            });

            return lstAll;
        }
    }
}
