using Microsoft.AspNetCore.Mvc;

namespace QuotationMaker.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private authToken _objToken;

        public HomeController(ILogger<HomeController> logger, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;

            this._httpContextAccessor = httpContextAccessor;

            this._objToken = new authToken(this._httpContextAccessor);
        }

        public IActionResult ModalList()
        {
            if (checkToken() == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return Redirect("~/Home/Login");
            }

            if (this._objToken.user_perm != "system")
            {
                return Redirect("~/");
            }

            return View();
        }

        public IActionResult Login()
        {
            return View();
        }

        public IActionResult CompanyList()
        {
            if (checkToken() == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return Redirect("~/Home/Login");
            }


            return View();
        }

        public IActionResult RateList()
        {
            if (checkToken() == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return Redirect("~/Home/Login");
            }

            if (this._objToken.user_perm != "system")
            {
                return Redirect("~/");
            }

            return View();
        }

        public IActionResult GroupList()
        {
            if (checkToken() == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return Redirect("~/Home/Login");
            }

            if (this._objToken.user_perm != "system")
            {
                return Redirect("~/");
            }

            return View();
        }

        public IActionResult UserList()
        {
            if (checkToken() == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return Redirect("~/Home/Login");
            }

            if (this._objToken.user_perm != "system")
            {
                return Redirect("~/");
            }

            return View();
        }
        public IActionResult ProjectList()
        {
            if (checkToken() == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return Redirect("~/Home/Login");
            }

            
            return View();
        }

        public Boolean checkToken()
        {
            this._objToken = new authToken(this._httpContextAccessor);

            if (this._objToken.user_isLogin == false)
            {
                HttpContext.Response.Cookies.Delete("token_key");
                return false;
            }

            @ViewData["user_name"] = this._objToken.user_name;
            ViewData["user_permtype"] = "一般使用者";
            if (this._objToken.user_perm == "system")
            {
                ViewData["authMenu"] = "display:block;";
                ViewData["user_permtype"] = "管理者";
            }
            else
            {
                ViewData["authMenu"] = "display:none;";
            }

            return true;
        }
    }
}
