package org.wfc.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.wfc.common.core.utils.PageUtils;
import org.wfc.common.core.utils.ResponseUtils;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.omada.api.sitesetting.OmadaAuthenticationApi;
import org.wfc.omada.api.sitesetting.OmadaWirelessNetworkApi;
import org.wfc.omada.api.sitesetting.model.CreateSsidOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseGridVoSsidOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseListPortalResOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseListWlanGroupOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseListWlanSimpleOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseObject;
import org.wfc.omada.api.sitesetting.model.OperationResponseSsidDetailOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseWithoutResult;
import org.wfc.omada.api.sitesetting.model.PortalResOpenApiVo;
import org.wfc.omada.api.sitesetting.model.SsidDetailOpenApiVo;
import org.wfc.omada.api.sitesetting.model.SsidOpenApiVo;
import org.wfc.omada.api.sitesetting.model.UpdateSsidBasicConfigOpenApiVo;
import org.wfc.omada.api.sitesetting.model.WlanGroupOpenApiVo;
import org.wfc.omada.api.sitesetting.model.WlanSimpleOpenApiVo;
import org.wfc.system.domain.vo.SsidOpenApiExtVo;
import org.wfc.system.service.ISysWlanService;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description: wlan service impl
 * @author: cyc
 * @since: 2025-02-15
 */
@Slf4j
@Service
public class SysWlanServiceImpl implements ISysWlanService {

    @Autowired
    private OmadaWirelessNetworkApi omadaWirelessNetworkApi;

    @Autowired
    private OmadaAuthenticationApi omadaAuthenticationApi;

    @Override
    public List<WlanGroupOpenApiVo> getWlanGroupList(String siteId) {
        ResponseEntity<OperationResponseListWlanGroupOpenApiVo> response = omadaWirelessNetworkApi.getWlanGroupList(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return response.getBody().getResult();
    }

    @Override
    public TableDataInfo getSsidList(String siteId, String wlanId, Integer pageNum, Integer pageSize) {
        ResponseEntity<OperationResponseGridVoSsidOpenApiVo> response = omadaWirelessNetworkApi.getSsidList(siteId, wlanId, pageNum, pageSize);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());


        ResponseEntity<OperationResponseListPortalResOpenApiVo> portalResponse = omadaAuthenticationApi.getPortalList(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(portalResponse.getBody()).getErrorCode(), portalResponse.getBody().getMsg());

        // 组装portalName
        List<PortalResOpenApiVo> portals = portalResponse.getBody().getResult();
        List<SsidOpenApiVo> data = response.getBody().getResult().getData();
        List<SsidOpenApiExtVo> ssidList = data.stream().map(c -> {
            SsidOpenApiExtVo ssidExtVo = new SsidOpenApiExtVo();
            BeanUtils.copyProperties(c, ssidExtVo);
            Optional<PortalResOpenApiVo> portalOptional = portals.stream().filter(p -> p.getSsidList().contains(c.getSsidId())).findFirst();
            ssidExtVo.setPortalEnable(false);
            if (portalOptional.isPresent()) {
                ssidExtVo.setPortalEnable(true);
                ssidExtVo.setPortalName(portalOptional.get().getName());
            }
            return ssidExtVo;
        }).collect(Collectors.toList());


        return PageUtils.getDataTable(ssidList, response.getBody().getResult().getTotalRows());
    }

    @Override
    public boolean createSsid(String siteId, String wlanId, CreateSsidOpenApiVo createSsidOpenApiVo) {
        ResponseEntity<OperationResponseObject> response = omadaWirelessNetworkApi.createSsid(siteId, wlanId, createSsidOpenApiVo);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

    @Override
    public boolean updateSsidBasicConfig(String siteId, String wlanId, String ssidId, UpdateSsidBasicConfigOpenApiVo updateSsidBasicConfigOpenApiVo) {
        ResponseEntity<OperationResponseWithoutResult> response = omadaWirelessNetworkApi.updateSsidBasicConfig(siteId, wlanId, ssidId, updateSsidBasicConfigOpenApiVo);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

    @Override
    public boolean deleteSsid(String siteId, String wlanId, String ssidId) {
        ResponseEntity<OperationResponseWithoutResult> response = omadaWirelessNetworkApi.deleteSsid(siteId, wlanId, ssidId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

    @Override
    public SsidDetailOpenApiVo getSsidDetail(String siteId, String wlanId, String ssidId) {
        ResponseEntity<OperationResponseSsidDetailOpenApiVo> response = omadaWirelessNetworkApi.getSsidDetail(siteId, wlanId, ssidId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return response.getBody().getResult();
    }

    @Override
    public List<WlanSimpleOpenApiVo> getMacAuthSsids(String siteId) {
        ResponseEntity<OperationResponseListWlanSimpleOpenApiVo> response = omadaAuthenticationApi.getMacAuthSsids(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return response.getBody().getResult();
    }
}
