package com.purui.prims.headless.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.purui.prims.headless.server.service.CanvasService;
import com.purui.prims.headless.server.service.DimensionService;
import com.purui.prims.headless.server.service.MetricService;
import com.purui.prims.headless.server.service.ModelService;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.enums.AuthType;
import com.purui.prims.headless.api.pojo.request.CanvasReq;
import com.purui.prims.headless.api.pojo.response.CanvasSchemaResp;
import com.purui.prims.headless.api.pojo.response.DimensionResp;
import com.purui.prims.headless.api.pojo.response.MetricResp;
import com.purui.prims.headless.api.pojo.response.ModelResp;
import com.purui.prims.headless.server.persistence.dataobject.CanvasDO;
import com.purui.prims.headless.server.persistence.mapper.CanvasDOMapper;
import com.purui.prims.headless.server.pojo.MetaFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CanvasServiceImpl extends ServiceImpl<CanvasDOMapper, CanvasDO> implements CanvasService {

    @Autowired
    private ModelService modelService;

    @Autowired
    private DimensionService dimensionService;

    @Autowired
    private MetricService metricService;

    @Override
    public List<CanvasDO> getCanvasList(Long domainId) {
        QueryWrapper<CanvasDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CanvasDO::getDomainId, domainId);
        return list(queryWrapper);
    }

    @Override
    public List<CanvasSchemaResp> getCanvasSchema(Long domainId, User user) {
        List<CanvasSchemaResp> canvasSchemaResps = Lists.newArrayList();
        List<ModelResp> modelResps = modelService.getModelListWithAuth(user, domainId, AuthType.ADMIN);
        for (ModelResp modelResp : modelResps) {
            CanvasSchemaResp canvasSchemaResp = new CanvasSchemaResp();
            MetaFilter metaFilter = new MetaFilter();
            metaFilter.setModelIds(Lists.newArrayList(modelResp.getId()));
            List<MetricResp> metricResps = metricService.getMetrics(metaFilter);
            List<DimensionResp> dimensionResps = dimensionService.getDimensions(metaFilter);
            canvasSchemaResp.setModel(modelResp);
            canvasSchemaResp.setDimensions(dimensionResps);
            canvasSchemaResp.setMetrics(metricResps);
            canvasSchemaResp.setDomainId(domainId);
            canvasSchemaResps.add(canvasSchemaResp);
        }
        return canvasSchemaResps;
    }

    @Override
    public CanvasDO createOrUpdateCanvas(CanvasReq canvasReq, User user) {
        if (canvasReq.getId() == null) {
            canvasReq.createdBy(user.getUserName());
            CanvasDO viewInfoDO = new CanvasDO();
            BeanUtils.copyProperties(canvasReq, viewInfoDO);
            save(viewInfoDO);
            return viewInfoDO;
        }
        Long id = canvasReq.getId();
        CanvasDO viewInfoDO = getById(id);
        canvasReq.updatedBy(user.getUserName());
        BeanUtils.copyProperties(canvasReq, viewInfoDO);
        updateById(viewInfoDO);
        return viewInfoDO;
    }

    @Override
    public void deleteCanvas(Long id) {
        removeById(id);
    }

}
