using IdentityServer4;
using Kimuser.Dto.Models.SystemUsers;
using Kimuser.Dto.SmartTable;
using Kimuser.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Kimuser.Controllers
{
    [Authorize(Roles = "Admin")]
    public class LocalSystemUserController : Controller
    {
        private readonly ISystemUserService _systemUserService;
        private readonly IdentityServerTools _tools;
        public LocalSystemUserController(ISystemUserService systemUserService, IdentityServerTools tools)
        {
            _systemUserService = systemUserService;
            _tools = tools;
        }


        public async Task<IActionResult> GetToken()
        {
            var token = await _tools.IssueClientJwtAsync(
                                clientId: "HeadQuaterClient",
                                lifetime: 3600,
                                audiences: new[] { "BackEnd" },
                                scopes: new[] { "BackEnd", "IdentityServerApi" });
            return Json(token);
        }


        [HttpPost]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var systemUsers = await _systemUserService.LocalSearch(param);
            return Json(systemUsers);
        }

        [HttpPost]
        public async Task<IActionResult> SetPassword([FromBody] ChangePasswordModel model)
        {
            var result = await _systemUserService.SetPassword(model);
            return Json(result);
        }

        [HttpPost]
        public async Task<IActionResult> Lock([FromBody] LockModel model)
        {
            var result = await _systemUserService.Lock(model);
            return Json(result);
        }

        [HttpPost]
        public async Task<IActionResult> UnLock([FromBody] LockModel model)
        {
            var result = await _systemUserService.UnLock(model);
            return Json(result);
        }

        [HttpPost]
        public async Task<IActionResult> CreateOrUpdate([FromBody] SystemUserModel model)
        {
            var result = await _systemUserService.CreateOrUpdate(model);
            return Json(result);
        }


    }
}
