package top.continew.admin.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.business.mapper.EffectTemplateMapper;
import top.continew.admin.business.model.entity.EffectTemplate;
import top.continew.admin.business.model.query.EffectTemplatePageQuery;
import top.continew.admin.business.model.query.EffectTemplateQuery;
import top.continew.admin.business.model.req.EffectTemplateReq;
import top.continew.admin.business.model.resp.EffectTemplateDetailResp;
import top.continew.admin.business.model.resp.EffectTemplateResp;
import top.continew.admin.business.service.EffectTemplateService;
import top.continew.admin.system.service.FileService;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

@Service
@RequiredArgsConstructor
public class EffectTemplateServiceImpl extends BaseServiceImpl<EffectTemplateMapper, EffectTemplate, EffectTemplateResp, EffectTemplateDetailResp, EffectTemplateQuery, EffectTemplateReq> implements EffectTemplateService {

    private final EffectTemplateMapper effectTemplateMapper;
    private final FileService fileService;

    @Override
    public PageResp<EffectTemplateResp> pageList(EffectTemplateQuery query, EffectTemplatePageQuery pageQuery) {
        LambdaQueryWrapper<EffectTemplate> wrapper = new LambdaQueryWrapper<>();
        if (query.getName() != null) {
            wrapper.like(EffectTemplate::getName, query.getName());
        }
        if (query.getType() != null) {
            wrapper.eq(EffectTemplate::getType, query.getType());
        }
        if (query.getIsCustom() != null) {
            wrapper.eq(EffectTemplate::getIsCustom, query.getIsCustom());
        }
        //query.setUserId(LoginHelper.getUserId().toString());
        if (query.getUserId() != null) {
            wrapper.eq(EffectTemplate::getUserId, query.getUserId());
        }

        wrapper.orderByDesc(EffectTemplate::getCreateTime);

        Page<EffectTemplate> page = new Page<>(pageQuery.getPage(), pageQuery.getSize());
        Page<EffectTemplate> result = effectTemplateMapper.selectPage(page, wrapper);

        PageResp<EffectTemplateResp> pageResp = new PageResp();
        pageResp.setTotal(result.getTotal());
        pageResp.setList(result.getRecords().stream().map(this::convertToResp).toList());
        return pageResp;
    }

    @Override
    public Long add(EffectTemplateReq req) {
        EffectTemplate effectTemplate = new EffectTemplate();
        BeanUtils.copyProperties(req, effectTemplate);
        effectTemplate.setCreateTime(java.time.LocalDateTime.now());
        effectTemplate.setUpdateTime(java.time.LocalDateTime.now());
        effectTemplateMapper.insert(effectTemplate);
        return effectTemplate.getId();
    }

    @Override
    public Long uploadTemplate(MultipartFile file, String name, String type, String userId) {
        if (file == null || file.isEmpty()) {
            throw new IllegalArgumentException("上传文件不能为空");
        }

        // 保存文件
        FileInfo uploadResult = fileService.upload(file);

        // 创建特效模板记录
        EffectTemplate effectTemplate = new EffectTemplate();
        effectTemplate.setName(name);
        effectTemplate.setType(type);
        effectTemplate.setUserId(userId);
        effectTemplate.setIsCustom("true"); // 标记为自定义模板
        effectTemplate.setSourceUrl(uploadResult.getUrl());
        effectTemplate.setCreateTime(java.time.LocalDateTime.now());
        effectTemplate.setUpdateTime(java.time.LocalDateTime.now());

        effectTemplateMapper.insert(effectTemplate);
        return effectTemplate.getId();
    }

    @Override
    public void update(EffectTemplateReq req, Long id) {
        EffectTemplate effectTemplate = new EffectTemplate();
        BeanUtils.copyProperties(req, effectTemplate);
        effectTemplate.setId(id);
        effectTemplate.setUpdateTime(java.time.LocalDateTime.now());
        effectTemplateMapper.updateById(effectTemplate);
    }

    private EffectTemplateResp convertToResp(EffectTemplate effectTemplate) {
        EffectTemplateResp resp = new EffectTemplateResp();
        BeanUtils.copyProperties(effectTemplate, resp);
        return resp;
    }
}