package cn.stylefeng.team.form.modular.component.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.form.modular.component.entity.FormComponent;
import cn.stylefeng.team.form.modular.component.entity.FormComponentCategory;
import cn.stylefeng.team.form.modular.component.enums.FormComponentCategoryExceptionEnum;
import cn.stylefeng.team.form.modular.component.mapper.FormComponentCategoryMapper;
import cn.stylefeng.team.form.modular.component.pojo.request.FormComponentCategoryRequest;
import cn.stylefeng.team.form.modular.component.pojo.request.FormComponentRequest;
import cn.stylefeng.team.form.modular.component.pojo.response.SummaryFormComponent;
import cn.stylefeng.team.form.modular.component.service.FormComponentCategoryService;
import cn.stylefeng.team.form.modular.component.service.FormComponentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class FormComponentCategoryServiceImpl extends ServiceImpl<FormComponentCategoryMapper, FormComponentCategory> implements FormComponentCategoryService {

    @Resource
    private FormComponentService formComponentService;

    @Override
    public void add(FormComponentCategoryRequest formComponentCategoryRequest) {
        FormComponentCategory formComponentCategory = new FormComponentCategory();
        BeanUtil.copyProperties(formComponentCategoryRequest, formComponentCategory);
        this.save(formComponentCategory);
    }

    @Override
    public void del(FormComponentCategoryRequest formComponentCategoryRequest) {
        FormComponentCategory formComponentCategory = this.queryFormComponentCategory(formComponentCategoryRequest);
        this.removeById(formComponentCategory.getComponentCategoryId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(FormComponentCategoryRequest formComponentCategoryRequest) {
        this.removeByIds(formComponentCategoryRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(FormComponentCategoryRequest formComponentCategoryRequest) {
        FormComponentCategory formComponentCategory = this.queryFormComponentCategory(formComponentCategoryRequest);
        BeanUtil.copyProperties(formComponentCategoryRequest, formComponentCategory);
        this.updateById(formComponentCategory);
    }

    @Override
    public FormComponentCategory detail(FormComponentCategoryRequest formComponentCategoryRequest) {
        return this.queryFormComponentCategory(formComponentCategoryRequest);
    }

    @Override
    public List<FormComponentCategory> findList(FormComponentCategoryRequest formComponentCategoryRequest) {
        LambdaQueryWrapper<FormComponentCategory> wrapper = createWrapper(formComponentCategoryRequest);
        return this.list(wrapper);
    }

    @Override
    public PageResult<FormComponentCategory> findPage(FormComponentCategoryRequest formComponentCategoryRequest) {
        LambdaQueryWrapper<FormComponentCategory> wrapper = createWrapper(formComponentCategoryRequest);
        Page<FormComponentCategory> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public SummaryFormComponent findTotalList(FormComponentCategoryRequest formComponentCategoryRequest) {

        LambdaQueryWrapper<FormComponentCategory> wrapper = this.createWrapper(formComponentCategoryRequest);
        List<FormComponentCategory> sumCategoryList = this.list(wrapper);

        FormComponentRequest formComponentRequest = new FormComponentRequest();
        List<FormComponent> pcComponentList = formComponentService.findList(formComponentRequest);

        List<FormComponent> mobileComponentList = new ArrayList<>();
        for (FormComponent formComponent : pcComponentList) {
            if (formComponent.getMobileEnableFlag() != null && formComponent.getMobileEnableFlag()) {
                mobileComponentList.add(formComponent);
            }
        }

        for (FormComponentCategory formComponentCategory : sumCategoryList) {
            List<FormComponent> items = new ArrayList<>();
            for (FormComponent formComponent : pcComponentList) {
                if (formComponent.getComponentCategoryId().equals(formComponentCategory.getComponentCategoryId())) {
                    items.add(formComponent);
                }
            }
            formComponentCategory.setFormComponentList(items);
        }
        SummaryFormComponent summaryFormComponent = new SummaryFormComponent();
        summaryFormComponent.setPcFormComponent(sumCategoryList);

        List<FormComponentCategory> mobileCategoryList = new ArrayList<>();
        for (FormComponentCategory formComponentCategory : sumCategoryList) {
            List<FormComponent> items = new ArrayList<>();
            for (FormComponent formComponent : mobileComponentList) {
                if (formComponent.getComponentCategoryId().equals(formComponentCategory.getComponentCategoryId())) {
                    items.add(formComponent);
                }
            }
            if (ObjectUtil.isNotEmpty(items)) {
                FormComponentCategory tempCategory = new FormComponentCategory();
                tempCategory.setComponentCategoryId(formComponentCategory.getComponentCategoryId());
                tempCategory.setCategoryName(formComponentCategory.getCategoryName());
                tempCategory.setCategoryCode(formComponentCategory.getCategoryCode());
                tempCategory.setFldSort(formComponentCategory.getFldSort());
                tempCategory.setFormComponentList(items);
                mobileCategoryList.add(tempCategory);
            }
        }
        summaryFormComponent.setMobileFormComponent(mobileCategoryList);

        return summaryFormComponent;
    }

    private FormComponentCategory queryFormComponentCategory(FormComponentCategoryRequest formComponentCategoryRequest) {
        FormComponentCategory formComponentCategory = this.getById(formComponentCategoryRequest.getComponentCategoryId());
        if (ObjectUtil.isEmpty(formComponentCategory)) {
            throw new ServiceException(FormComponentCategoryExceptionEnum.FORM_COMPONENT_CATEGORY_NOT_EXISTED);
        }
        return formComponentCategory;
    }

    private LambdaQueryWrapper<FormComponentCategory> createWrapper(FormComponentCategoryRequest formComponentCategoryRequest) {
        LambdaQueryWrapper<FormComponentCategory> queryWrapper = new LambdaQueryWrapper<>();

        String searchText = formComponentCategoryRequest.getSearchText();
        if (StrUtil.isNotEmpty(searchText)) {
            queryWrapper.like(FormComponentCategory::getCategoryName, searchText);
        }

        return queryWrapper;
    }

}
