package com.yc.ship.module.otc.service.print;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.otc.controller.admin.print.vo.PrintPageReqVO;
import com.yc.ship.module.otc.controller.admin.print.vo.PrintSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.print.PrintDO;
import com.yc.ship.module.otc.dal.mysql.print.PrintMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.PRINT_NOT_EXISTS;


/**
 * 打印模板 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class PrintServiceImpl implements PrintService {

    @Resource
    private PrintMapper printMapper;

    @Override
    public Long createPrint(PrintSaveReqVO createReqVO) {
        // 插入
        PrintDO print = BeanUtils.toBean(createReqVO, PrintDO.class);
        print.setId(IdWorker.getId(print));
        print.setContent(convertTempContent(createReqVO.getTempData()));
        printMapper.insert(print);
        // 返回
        return print.getId();
    }

    @Override
    public void updatePrint(PrintSaveReqVO updateReqVO) {
        // 校验存在
        validatePrintExists(updateReqVO.getId());
        // 更新
        PrintDO updateObj = BeanUtils.toBean(updateReqVO, PrintDO.class);
        updateObj.setContent(convertTempContent(updateReqVO.getTempData()));
        printMapper.updateById(updateObj);
    }

    @Override
    public void deletePrint(Long id) {
        // 校验存在
        validatePrintExists(id);
        // 删除
        printMapper.deleteById(id);
    }

    private void validatePrintExists(Long id) {
        if (printMapper.selectById(id) == null) {
            throw exception(PRINT_NOT_EXISTS);
        }
    }

    @Override
    public PrintDO getPrint(Long id) {
        return printMapper.selectById(id);
    }

    @Override
    public PageResult<PrintDO> getPrintPage(PrintPageReqVO pageReqVO) {
        return printMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PrintDO> getPrintList() {
        return printMapper.selectList(PrintDO::getIsUse, true, PrintDO::getDeleted, false);
    }

    private String convertTempContent(String tempData) {
        JSONArray jsonArray = new JSONArray();
        if (StringUtils.isNotBlank(tempData)) {
            JSONObject jsonObject = new JSONObject(tempData);
            JSONArray panels = jsonObject.getJSONArray("panels");
            for (int i = 0; i < panels.size(); i++) {
                JSONObject panel = panels.getJSONObject(i);
                JSONArray printElements = panel.getJSONArray("printElements");
                JSONObject data;
                for (int j = 0; j < printElements.size(); j++) {
                    data = new JSONObject();
                    JSONObject printElement = printElements.getJSONObject(j);
                    JSONObject printElementType = printElement.getJSONObject("printElementType");
                    JSONObject options = printElement.getJSONObject("options");
                    data.putAll(options);
                    String type = printElementType.getStr("type", "");
                    if (StringUtils.equals("text", type)) {
                        String textType = data.getStr("textType", "");
                        String fontSize = data.getStr("fontSize");
                        String title = data.getStr("title", "");
                        data.set("content", title + data.getStr("testData"));
                        data.remove("testData");
                        data.remove("field");
                        data.remove("fontSize");
                        data.remove("lineHeight");
                        data.remove("textType");
                        data.remove("title");
                        data.remove("qid");
                        data.remove("coordinateSync");
                        data.remove("widthHeightSync");
                        data.remove("qrCodeLevel");
                        switch (textType) {
                            case "qrcode":
                                // 二维码
                                data.set("contentType", "二维码");
                                break;
                            case "barcode":
                                // 条形码
                                data.set("contentType", "条形码");
                                break;
                            default:
                                // 文本
                                data.set("contentType", "文本");
                                String fontName = data.getStr("fontFamily", "宋体");
                                data.set("fontName", fontName);
                                data.set("fontSize", fontSize);
                                Integer fontWeight = data.getInt("fontWeight", 500);
                                data.set("isStrong", fontWeight > 500 ? 1 : 0);
                                data.set("isTilt", 0);

                                data.remove("title");
                                data.remove("textAlign");
                                data.remove("fontWeight");
                                data.remove("fontFamily");
                                data.remove("textContentVerticalAlign");
                        }
                    }
                    if (StringUtils.equals("image", type)) {
                        String src = data.getStr("src");
                        data.remove("testData");
                        data.remove("field");
                        data.remove("src");
                        data.set("content", src);
                        data.set("contentType", "图片");
                    }
                    jsonArray.add(data);
                }
            }

        }
        return jsonArray.toString();
    }

}
