package com.tuoheng.admin.entity.request;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.List;
import java.util.Properties;

/**
 * @Author ChengWang
 * @Date 2023/5/24
 */
public class VoteTitleRequest implements CellWriteHandler {

    private String title;

    PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}");

    public VoteTitleRequest(String title) {
        this.title = title;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        if(head != null){
            List<String> headNameList = head.getHeadNameList();
            if(CollectionUtil.isNotEmpty(headNameList)){
                Properties properties = new Properties();
                properties.setProperty("title", title);
                for (int i = 0; i < headNameList.size(); i++) {
                    headNameList.set(i, placeholderHelper.replacePlaceholders(headNameList.get(i), properties));
                }
            }
        }

    }
}
