package org.wfc.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.core.web.form.WANFiRedirectParams;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.omada.api.client.OmadaClientApi;
import org.wfc.omada.api.client.model.OperationResponseClientDetail;
import org.wfc.omada.api.hotspot.OmadaAuthorizedClientApi;
import org.wfc.user.api.domain.UUser;

import java.util.Objects;

/**
 * 客户端操作处理
 *
 * @author wfc
 */
@RestController
@RequestMapping("/client")
public class ClientController extends BaseController {
    @Autowired
    private OmadaClientApi omadaClientApi;

    @Autowired
    private OmadaAuthorizedClientApi omadaAuthorizedClientApi;

    /**
     * 获取客户信息
     */
    //    @RequiresLogin()
    @GetMapping("/auth")
    public AjaxResult auth() {
        LoginUser<UUser> loginUser = SecurityUtils.getLoginUser();
        WANFiRedirectParams wanFiRedirectParams = loginUser.getWanFiRedirectParams();
        if (wanFiRedirectParams == null) {
            return error(MessageUtils.message("user.client.not.redirect.params"));
        }
        String site = wanFiRedirectParams.getSite();
        String clientMac = wanFiRedirectParams.getClientMac();
        if (StringUtils.isBlank(site) || StringUtils.isBlank(clientMac)) {
//            return error("not mac address");
        }
//        ResponseEntity<OperationResponseWithoutResult> posts = omadaAuthorizedClientApi.authClient(site, clientMac);
//        return success(Objects.requireNonNull(posts.getBody()).getMsg());
        return success();
    }

    /**
     * 获取客户信息
     */
    //    @RequiresLogin()
    @GetMapping("/unauth")
    public AjaxResult unauth() {
        LoginUser<UUser> loginUser = SecurityUtils.getLoginUser();
        WANFiRedirectParams wanFiRedirectParams = loginUser.getWanFiRedirectParams();
        if (wanFiRedirectParams == null) {
            return error(MessageUtils.message("user.client.not.redirect.params"));
        }
        String site = wanFiRedirectParams.getSite();
        String clientMac = wanFiRedirectParams.getClientMac();
        if (StringUtils.isBlank(site) || StringUtils.isBlank(clientMac)) {
//            return error("not mac address");
        }
//        ResponseEntity<OperationResponseWithoutResult> posts = omadaAuthorizedClientApi.cancelAuthClient(site, clientMac);
//        return success(Objects.requireNonNull(posts.getBody()).getMsg());
        return success();
    }

    /**
     * 获取客户信息
     */
    //    @RequiresLogin()
    @GetMapping("/info")
    public AjaxResult info() {
        LoginUser<UUser> loginUser = SecurityUtils.getLoginUser();
        WANFiRedirectParams wanFiRedirectParams = loginUser.getWanFiRedirectParams();
        if (wanFiRedirectParams == null) {
//            return error("not wanfi redirect params");
        }
        String site = wanFiRedirectParams.getSite();
        String clientMac = wanFiRedirectParams.getClientMac();
        if (StringUtils.isBlank(site) || StringUtils.isBlank(clientMac)) {
//            return error("not mac address");
        }
        ResponseEntity<OperationResponseClientDetail> posts = omadaClientApi.getClientDetail(site, clientMac);
        return success(Objects.requireNonNull(posts.getBody()).getResult());
    }

}
