using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Catalog26.Data;
using PricingCatalog.Models;
using Microsoft.IdentityModel.Tokens;

namespace Catalog26.Pages.TDSynnex;

public class IndexModel : PageModel
{
    private readonly Catalog26Context _context;
    private readonly IConfiguration Configuration;

    public IndexModel(Catalog26Context context, IConfiguration configuration)
    {
        _context = context;
        Configuration = configuration;
    }

    //public string MfgPartNoSort { get; set; }
    //public string VendorPartNoSort { get; set; }
    public string CurrentFilter { get; set; }
    public string FilterBy { get; set; }
    //public string CurrentSort { get; set; }

    public PaginatedList<TDSPriceAvailability> TDSPriceAvailability { get; set; }
    //public IList<TDSPriceAvailability> TDSPriceAvailability { get;set; }

    //public async Task OnGetAsync(string sortOrder, string currentFilter,
    //    string searchString, string filterBy, int? pageIndex)
    public async Task OnGetAsync(string currentFilter,
        string searchString, string filterBy, int? pageIndex)
    {
        // TDSPriceAvailability = await _context.TDSPriceAvailability.ToListAsync();
        // using System;
        //MfgPartNoSort = String.IsNullOrEmpty(sortOrder) ? "mfgpartno_desc" : "";
        //VendorPartNoSort = String.IsNullOrEmpty(sortOrder) ? "vendorpartno_desc" : "vendorpartno_asc";

        if (searchString != null)
        {
            pageIndex = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        CurrentFilter = searchString;
        FilterBy = filterBy;

        IQueryable<TDSPriceAvailability> tdsPriceAvailabilityIQ = from s in _context.TDSCatalogItems
                                                                  select s;

        if (!String.IsNullOrEmpty(searchString))
            {
                //tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.Where(s => s.MfgPartNo.Contains(searchString)
                //    || s.VendorPartNo.Contains(searchString));

            switch (filterBy)
            {
                case "mfgpartno":
                    tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.Where(s => s.MfgPartNo.Contains(searchString));
                    break;
                case "vendorpartno":
                    tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.Where(s => s.VendorPartNo.Contains(searchString));
                    break;
                case "mfgname":
                    tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.Where(s => s.MfgName.Contains(searchString));
                    break;
                case "description":
                    tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.Where(s => s.PartDescription.Contains(searchString));
                    break;
            }
        }

        /* --
        switch (sortOrder)
        {
            case "mfgpartno_desc":
                tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.OrderByDescending(s => s.MfgPartNo);
                break;
            case "vendorpartno_desc":
                tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.OrderByDescending(s => s.VendorPartNo);
                break;
            case "vendorpartno_asc":
                tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.OrderBy(s => s.VendorPartNo);
                break;
            default:
                tdsPriceAvailabilityIQ = tdsPriceAvailabilityIQ.OrderBy(s => s.MfgPartNo);
                break;
        }
        -- */

        var pageSize = Configuration.GetValue("PageSize", 20);
        // TDSPriceAvailabilities = await tdsPriceAvailabilityIQ.AsNoTracking().ToListAsync();
        TDSPriceAvailability = await PaginatedList<TDSPriceAvailability>.CreateAsync(
            tdsPriceAvailabilityIQ.AsNoTracking(), pageIndex ?? 1, pageSize);
    }
}
