package com.ruoyi.common.auth.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.auth.entity.OauthClientDetails;
import com.ruoyi.common.auth.service.OauthClientDetailsService;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author Yuuki
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("client")
public class OauthClientDetailsController extends BaseController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        //noinspection RedundantArrayCreation
        binder.setDisallowedFields(new String[]{"admin"});
    }

    private final OauthClientDetailsService oauthClientDetailsService;

    @GetMapping("exist/{clientId}")
    public boolean checkUserName(@NotBlank(message = "{required}") @PathVariable String clientId) {
        OauthClientDetails client = this.oauthClientDetailsService.findById(clientId);
        return client == null;
    }

    @GetMapping("secret/{clientId}")
    @RequiresPermissions("client:decrypt")
    public AjaxResult getOriginClientSecret(@NotBlank(message = "{required}") @PathVariable String clientId) {
        OauthClientDetails client = this.oauthClientDetailsService.findById(clientId);
        String origin = client != null ? client.getOriginSecret() : StringUtils.EMPTY;
        return AjaxResult.success(origin);
    }

    @GetMapping("/list")
    @RequiresPermissions("client:view")
    public TableDataInfo oauthCliendetailsList(OauthClientDetails oAuthClientDetails) {
        startPage();
        List<OauthClientDetails> list = oauthClientDetailsService.findOauthClientDetails(oAuthClientDetails);
        return getDataTable(list);
    }


    @PostMapping
    @RequiresPermissions("client:add")
    public void addOauthCliendetails(@Valid @RequestBody OauthClientDetails oAuthClientDetails) throws ServiceException {
        try {
            this.oauthClientDetailsService.createOauthClientDetails(oAuthClientDetails);
        } catch (Exception e) {
            String message = "新增客户端失败";
            log.error(message, e);
            throw new ServiceException(message);
        }
    }

    @DeleteMapping("deleteByIds/{clientIds}")
    @RequiresPermissions("client:delete")
    public void deleteOauthCliendetails(@PathVariable String clientIds) throws ServiceException {
        try {
            this.oauthClientDetailsService.deleteOauthClientDetails(clientIds);
        } catch (Exception e) {
            String message = "删除客户端失败";
            log.error(message, e);
            throw new ServiceException(message);
        }
    }

    @PutMapping
    @RequiresPermissions("client:update")
    public void updateOauthCliendetails(@Valid @RequestBody OauthClientDetails oAuthClientDetails) throws ServiceException {
        try {
            this.oauthClientDetailsService.updateOauthClientDetails(oAuthClientDetails);
        } catch (Exception e) {
            String message = "修改客户端失败";
            log.error(message, e);
            throw new ServiceException(message);
        }
    }

    @GetMapping("{clientId}")
    public OauthClientDetails getById(@NotBlank(message = "{required}") @PathVariable String clientId) {
        return this.oauthClientDetailsService.findById(clientId);
    }
}
