using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using Shride.Infrastructure;
using Shride.Models;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;

namespace Shride.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private IRideRepository repository;
        private IUserRepository uRepository;

        private UserManager<IdentityUser> userManager;
        private SignInManager<IdentityUser> signInManager;

        public HomeController(IRideRepository repo, IUserRepository uRep, UserManager<IdentityUser> uMgr, SignInManager<IdentityUser> sMgr)
        {
            repository = repo;
            uRepository = uRep;

            userManager = uMgr;
            signInManager = sMgr;
        }

        [AllowAnonymous]
        public IActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        public IActionResult About()
        {
            return View();
        }

        [AllowAnonymous]
        public ViewResult CreateUser(User user)
        {
            return View(user);
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ViewResult> AccountCreated(User userInput, Account account)
        {
            // verifiera mail och telefonnummer

            if (userInput != null)
            {
                IdentityUser user = new IdentityUser(userInput.Name);
                IdentityResult createdUser = await userManager.CreateAsync(user, userInput.Password);
                if (createdUser.Succeeded)
                {
                    await userManager.SetEmailAsync(user, userInput.Email);
                    await userManager.SetPhoneNumberAsync(user, userInput.Phone);

                    repository.AddAccount(account);
                }
            }
            return View(account);
        }

        [AllowAnonymous]
        public IActionResult Contact()
        {
            return View();
        }

        [AllowAnonymous]
        public ViewResult Login(string returnUrl)
        {
            return View(new User
            {
                ReturnUrl = returnUrl
            });
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(User user)
        {
            //IdentityUser activeUser = await userManager.FindByEmailAsync(user.Email);
            //IdentityUser currentUser = await uRepository.GetUser(user.Name);
            IdentityUser currentUser = await userManager.FindByNameAsync(user.Name);
            if(currentUser != null)
            {
                if (ModelState.IsValid)
                {
                    if (currentUser != null)
                    {
                        Console.WriteLine("If-satsen funkar");
                        await signInManager.SignOutAsync();
                        if ((await signInManager.PasswordSignInAsync(currentUser, user.Password, false, false)).Succeeded)
                        {
                            Console.WriteLine("Loggades in");
                            // Kolla vilken roll användaren har och ta den till rätt startsida
                            return Redirect("/User/Dashboard");
                        }
                    }
                }
            }
            
            ModelState.AddModelError("", "Incorrect");
            return View(user);
        }

        public async Task<IActionResult> Logout(string returnUrl = "/")
        {
            await signInManager.SignOutAsync();
            return Redirect(returnUrl);
        }

        [AllowAnonymous]
        public IActionResult SearchEvent()
        {
            IQueryable<Event> events = repository.SearchEvent();
            return View(events);
        }

        [AllowAnonymous]
        public IActionResult SearchRide()
        {
            IQueryable<Ride> rides = repository.SearchRide();
            return View(rides);
        }
    }
}
