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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.visitor.setting.VisitorSettingModel;
import com.zhongshu.card.client.model.visitor.setting.VisitorSettingParam;
import com.zhongshu.card.server.core.dao.org.RoleDao;
import com.zhongshu.card.server.core.dao.scene.SceneComponentDao;
import com.zhongshu.card.server.core.dao.visitor.VisitorSettingDao;
import com.zhongshu.card.server.core.domain.org.Role;
import com.zhongshu.card.server.core.domain.scene.SceneComponent;
import com.zhongshu.card.server.core.domain.visitor.VisitorSetting;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.user.RoleServiceImpl;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TRX
 * @date 2025/1/8
 */
@Slf4j
@Service
public class VisitorSettingService extends SuperService {

    @Autowired
    private VisitorSettingDao visitorSettingDao;

    @Autowired
    private SceneComponentDao sceneComponentDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleServiceImpl roleService;

    /**
     * 保存组件访客配置
     *
     * @param param
     * @return
     */
    public ResultContent saveInfo(VisitorSettingParam param) {
        SceneComponent sceneComponent = sceneComponentDao.findTopById(param.getSceneComponentId());
        if (ObjectUtils.isEmpty(sceneComponent)) {
            return ResultContent.buildFail("组件不存在");
        }
        if (param.getMinutes() == null || param.getMinutes() < 0) {
            return ResultContent.buildFail("授权时间不能为空或小于0");
        }
        String projectOid = param.getProjectOid();
        if (ObjectUtils.isEmpty(projectOid)) {
            return ResultContent.buildFail("projectOid 信息为空");
        }

        VisitorSetting visitorSetting = visitorSettingDao.findTopBySceneComponentIdAndProjectOid(sceneComponent.getId(), projectOid);
        if (ObjectUtils.isEmpty(visitorSetting)) {
            visitorSetting = new VisitorSetting();
            visitorSetting.setSceneComponentId(sceneComponent.getId());
            initEntityNoCheckOid(visitorSetting);
            visitorSetting.setProjectOid(projectOid);
        } else {
            initUpdateEntity(visitorSetting);
        }
        BeanUtils.copyProperties(param, visitorSetting);
        visitorSetting.setOid(projectOid);

        List<Role> visitorRoles = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(param.getVisitorRoleIds())) {
            visitorRoles = roleDao.findByIdIn(param.getVisitorRoleIds());
        }
        visitorSetting.setVisitorRoles(visitorRoles);
        visitorSettingDao.save(visitorSetting);
        return ResultContent.buildSuccess();
    }

    public ResultContent<VisitorSettingModel> getInfoByCode(String code, String projectOid) {
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = getCurrentProjectOid();
        }
        if (StringUtils.isEmpty(projectOid)) {
            return ResultContent.buildFail("projectOid 信息为空");
        }
        SceneComponent sceneComponent = sceneComponentDao.findTopByCode(code);
        if (ObjectUtils.isEmpty(sceneComponent)) {
            return ResultContent.buildFail(String.format("组件不存在：%s", code));
        }
        return getInfo(sceneComponent.getId(), projectOid);
    }

    /**
     * 得到组件访客配置
     *
     * @param sceneComponentId
     * @return
     */
    public ResultContent<VisitorSettingModel> getInfo(String sceneComponentId, String projectOid) {
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = getCurrentProjectOid();
        }
        if (StringUtils.isEmpty(projectOid)) {
            return ResultContent.buildFail("projectOid 信息为空");
        }
        VisitorSetting visitorSetting = visitorSettingDao.findTopBySceneComponentIdAndProjectOid(sceneComponentId, projectOid);
        return ResultContent.buildSuccess(toModel(visitorSetting));
    }

    public VisitorSettingModel toModel(VisitorSetting entity) {
        VisitorSettingModel model = new VisitorSettingModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);

            // 角色数据
            model.setVisitorRoles(roleService.toSimpleModels(entity.getVisitorRoles()));
        }
        return model;
    }
}
