﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Snapplicator.Areas.Profile.Models;
using Snapplicator.Areas.Profile.Services;
using Snapplicator.Controllers;
using Snapplicator.Models;
using Snapplicator.Services;

namespace Snapplicator.Areas.Profile.Controllers
{
    public class ProfileController : BaseController
    {
        private readonly IUserService _userService;
        private readonly IProfileFieldService _fieldService;

        public ProfileController(IUserService userService, IProfileFieldService fieldService)
        {
            _userService = userService;
            _fieldService = fieldService;
        }

        [Authorize]
        public ActionResult Index()
        {
            var id = User.Identity.GetUserId();

            return RedirectToAction("View", "Profile", new {id});
        }

        // GET: Profile/Profile
        public ActionResult View(string id)
        {
            var user = _userService.GetUserById(id);
            var fields = _fieldService.GetProfileFieldValuesForUserId(user.Id);

            ViewBag.Fields = fields;

            return View(user);
        }

        [Authorize]
        public ActionResult Edit()
        {
            var user = _userService.GetUserById(User.Identity.GetUserId());
            var userFields = _fieldService.GetProfileFieldValuesForUserId(user.Id).ToList();
            var fields = _fieldService.GetProfileFields().ToList();

            foreach (var field in fields)
            {
                if (!userFields.Any(x => x.ProfileFieldId == field.Id))
                {
                    var newValue = new ProfileFieldValue
                    {
                        ProfileFieldId = field.Id,
                        UserId = user.Id,
                        Value = ""
                    };

                    _fieldService.CreateProfileFieldValue(newValue);

                    userFields.Add(newValue);
                }
            }

            // Refetch with the proper virtual fields from any newly created ones
            // This could be done better
            userFields = _fieldService.GetProfileFieldValuesForUserId(user.Id).ToList();

            ViewBag.Fields = userFields;

            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            var user = _userService.GetUserById(collection["Id"]);
            user.FirstName = collection["FirstName"];
            user.LastName = collection["LastName"];
            user.InstagramUserName = collection["InstagramUserName"];
            user.TwitterUserName = collection["TwitterUserName"];
            user.FacebookUserName = collection["FacebookUserName"];

            _userService.UpdateUser(user);

            var fieldValues = _fieldService.GetProfileFieldValuesForUserId(collection["Id"]).ToList();
            foreach (var fieldValue in fieldValues)
            {
                var key = fieldValue.ProfileField.Name;

                if (collection.AllKeys.Contains(key))
                {
                    fieldValue.Value = collection[key];

                    // Dumb hack to fix a weird bug
                    if (fieldValue.Value.Trim() == ",")
                        fieldValue.Value = "";

                    _fieldService.UpdateProfileFieldValue(fieldValue);
                }
            }

            return Redirect("/Profile/View/" + collection["Id"] + "/");
        }
    }
}