package com.zhongshu.card.server.core.service.openAPI;

import com.github.microservice.auth.security.helper.AuthHelper;
import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.zhongshu.card.client.model.openApi.AppModel;
import com.zhongshu.card.client.model.openApi.AppParam;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.opengateway.client.model.OpenApiModel;
import com.zhongshu.opengateway.client.model.SignModel;
import com.zhongshu.opengateway.client.model.param.SignParam;
import com.zhongshu.opengateway.client.model.ret.ResultContent;
import com.zhongshu.opengateway.client.service.OpenApiService;
import com.zhongshu.opengateway.client.service.SignService;
import com.zhongshu.opengateway.client.type.AppState;
import com.zhongshu.opengateway.client.type.AppType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class OpenAppManagerService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    SignService signService;

    @Autowired
    OpenApiService openApiService;

    @Autowired
    AuthHelper authHelper;

    @Autowired
    OrganizationDao organizationDao;

    /**
     * 添加应用
     */
    public Object add(SignParam param) {
        return signService.add(param);
    }

    /**
     * 删除应用
     */
    public Object delete(String appId) {
        return signService.delete(appId);
    }

    /**
     * 禁用/启用
     */
    public Object disable(String appId, AppState appState) {
        return signService.disable(appId, appState);
    }

    /**
     * 分页获取该项目下的app列表
     */
    public Object page(AppParam param, Pageable pageable) {

        ResultContent<Page<SignModel>> ret = signService.pageByBusinessId(param.getBusinessId(), param.getAppType(), param.getAppState(), param.getAppName(), pageable.getPageNumber(), pageable.getPageSize());
        if (ret.isSuccess()){
            Page<SignModel> page = ret.getContent();
            if (!page.getContent().isEmpty()){
                return ResultContent.buildContent(PageEntityUtil.concurrent2PageModel(page, this::toAppModel));
            }
        }
        return ret;
    }

    private AppModel toAppModel(SignModel signModel){
        AppModel appModel = new AppModel();
        if (signModel!=null){
            BeanUtils.copyProperties(signModel, appModel, "appType", "appState");
            Organization organization = organizationDao.findTopByOid(signModel.getBusinessId());
            if (organization!=null && StringUtils.isNotEmpty(organization.getName())){
                appModel.setProjectName(organization.getName());
            }
            appModel.setAppType(signModel.getAppType().getRemark());
            appModel.setAppState(signModel.getAppState().getRemark());
        }
        return appModel;
    }
}
