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.model.OperationResponseListPortalResOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseListWlanSimpleOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponsePortalDetailResOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseWithoutResult;
import org.wfc.omada.api.sitesetting.model.PortalDetailResOpenApiVo;
import org.wfc.omada.api.sitesetting.model.PortalResOpenApiVo;
import org.wfc.omada.api.sitesetting.model.PortalSetting;
import org.wfc.omada.api.sitesetting.model.SsidSimpleOpenApiVo;
import org.wfc.omada.api.sitesetting.model.WlanSimpleOpenApiVo;
import org.wfc.system.domain.vo.PortalResOpenApiExtVo;
import org.wfc.system.service.ISysPortalService;

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

/**
 * @description: portal service impl
 * @author: cyc
 * @since: 2025-02-17
 */
@Slf4j
@Service
public class SysPortalServiceImpl implements ISysPortalService {

    @Autowired
    private OmadaAuthenticationApi omadaAuthenticationApi;

    @Override
    public TableDataInfo getPortalList(String siteId, Integer pageNum, Integer pageSize) {
        ResponseEntity<OperationResponseListPortalResOpenApiVo> response = omadaAuthenticationApi.getPortalList(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());

        List<PortalResOpenApiVo> result = response.getBody().getResult();
        List<PortalResOpenApiVo> pageResult = result.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());

        ResponseEntity<OperationResponseListWlanSimpleOpenApiVo> ssidsResponse = omadaAuthenticationApi.getMacAuthSsids(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(ssidsResponse.getBody()).getErrorCode(), ssidsResponse.getBody().getMsg());
        List<WlanSimpleOpenApiVo> wlans = ssidsResponse.getBody().getResult();
        List<SsidSimpleOpenApiVo> ssids = new ArrayList<>();
        for (WlanSimpleOpenApiVo wlan : wlans) {
            ssids.addAll(wlan.getSsidList());
        }

        // 组装ssidName
        List<PortalResOpenApiExtVo> portals = pageResult.stream().map(c -> {
            PortalResOpenApiExtVo portalExtVo = new PortalResOpenApiExtVo();
            BeanUtils.copyProperties(c, portalExtVo);
            List<String> ssidList = c.getSsidList();

            List<String> ssidNames = new ArrayList<>();
            for (String ssidId : ssidList) {
                for (SsidSimpleOpenApiVo ssid : ssids) {
                    if (Objects.equals(ssidId, ssid.getSsidId())) {
                        ssidNames.add(ssid.getSsidName());
                    }
                }
            }
            portalExtVo.setSsidNames(ssidNames);
            return portalExtVo;
        }).collect(Collectors.toList());

        return PageUtils.getDataTable(portals, (long) result.size());
    }

    @Override
    public boolean addPortal(String siteId, PortalSetting portalSetting) {
        ResponseEntity<OperationResponseWithoutResult> response = omadaAuthenticationApi.addPortal(siteId, portalSetting);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

    @Override
    public boolean modifyPortal(String siteId, String portalId, PortalSetting portalSetting) {
        ResponseEntity<OperationResponseWithoutResult> response = omadaAuthenticationApi.modifyPortal(siteId, portalId, portalSetting);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

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

    @Override
    public PortalDetailResOpenApiVo getPortalDetail(String siteId, String portalId) {
        ResponseEntity<OperationResponsePortalDetailResOpenApiVo> response = omadaAuthenticationApi.getPortalDetail(siteId, portalId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return response.getBody().getResult();
    }

}
