using System;
using System.Linq;
using System.Threading.Tasks;
using isnd.Data;
using isnd.Data.Catalog;
using isnd.Helpers;
using isnd.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace isnd.Controllers
{

    public partial class PackagesController
    {
        // Web search
        public async Task<IActionResult> Index(RegistrationPageIndexQuery model)
        {
            return View(new RegistrationPageIndexQueryAndResult{Query = model,
                Result = await packageManager.SearchPackageAsync(model)});
        }

        public async Task<IActionResult> Details(string pkgid)
        {
            if (pkgid == null)
            {
                return NotFound();
            }

            var packageVersion = dbContext.PackageVersions
                .Include(p => p.Package)
                .Where(m => m.PackageId == pkgid)
                .OrderByDescending(p => p)
                ;

            if (packageVersion.Count() == 0)
            {
                return NotFound();
            }
            bool results = await packageVersion.AnyAsync();
            var latest = await packageVersion.FirstAsync();

            return View("Details", new PackageDetailViewModel
            {
                ExternalUrl = isndSettings.ExternalUrl,
                latest = latest,
                pkgid = pkgid,
                totalHits = packageVersion.Count(),
                data = packageVersion.Take(MAX_PKG_VERSION_LIST).ToArray()
            });

        }
        const int MAX_PKG_VERSION_LIST = 50;

        [Authorize]
        public async Task<IActionResult> Delete(string pkgid, string version, string pkgtype)
        {
            if (pkgid == null || version == null)
            {
                return NotFound();
            }
            // var report = await packageManager.DeletePackageAsync(id, lower, type);

            var packageVersion = await dbContext.PackageVersions.Include(p => p.Package)
             .FirstOrDefaultAsync(m => m.PackageId == pkgid
             && m.FullString == version && m.Type == pkgtype);
            if (packageVersion == null) return NotFound();
            if (!User.IsOwner(packageVersion)) return Unauthorized();
            var pkg = await packageManager.GetPackageAsync(pkgid, version, pkgtype);
            return View(pkg);
        }

        // POST: PackageVersion/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(string PackageId, string FullString,
         string Type)
        {
            PackageVersion packageVersion = await dbContext.PackageVersions
             .Include(pv => pv.Package)
             .FirstOrDefaultAsync(m => m.PackageId == PackageId
             && m.FullString == FullString && m.Type == Type);
            if (packageVersion == null) return NotFound();
            if (!User.IsOwner(packageVersion)) return Unauthorized();

            await packageManager.DeletePackageAsync(PackageId, FullString, Type);
            return RedirectToAction(nameof(Index));
        }
    }

}