package cn.ezhizao.framework.qrCode;

import cn.ezhizao.common.utils.StringUtils;
import cn.ezhizao.common.utils.WaterMakerUtils;
import cn.ezhizao.common.utils.bean.ImgWaterMarker;
import cn.ezhizao.common.utils.bean.TextWaterMarker;
import cn.ezhizao.common.utils.bean.WaterMarker;
import cn.ezhizao.common.utils.file.FileUploadUtils;
import cn.ezhizao.framework.config.RuoYiConfig;
import cn.ezhizao.project.tenant.domain.BizTenant;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeException;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhushan
 */
@Service
@Slf4j
public class QrCodeService {
    @Resource
    private QrConfig config;

    @Resource
    private QrCoderDemo qrCoder;

    @Resource
    private WaterMakerUtils waterMakerUtils;
    /**
     *
     * @param content
     * @param filePath
     */
    public void createCodeToFile(String content, String filePath) {
        try {
            QrCodeUtil.generate(content,config, FileUtil.file(filePath));
        } catch (QrCodeException e) {
            e.printStackTrace();
        }
    }

    public void createNewCodeToFile(String content, String filePath,String footer) {
        BufferedImage bufferedImage = qrCoder.createQrCode(null, null, content, footer);
        qrCoder.createImage(bufferedImage, filePath);
    }

    /**
     *
     * @param content
     * @param response
     */

    public void createCodeToStream(String content, HttpServletResponse response) {
        try {
            QrCodeUtil.generate(content,config, "png", response.getOutputStream());
        } catch (QrCodeException | IOException e) {
            e.printStackTrace();
        }
    }


    // @Value("${identity.qrCode.company}")
    private String companyUrl;
    // @Value("${identity.qrCode.companyBackImg}")
    private String companyBackImg;
    /** 生成标识解析码 */

    public void generateIdentityQrCode(BizTenant bizTenant) {
        String content = companyUrl + bizTenant.getId();
        String qrCodePath = RuoYiConfig.getIdentityPath()+"/company/"+ bizTenant.getId()+".png";
        createCodeToFile(content,qrCodePath);
        WaterMarker waterMarker = new WaterMarker();
        waterMarker.setBackgroundImgUrl(RuoYiConfig.getIdentityPath() + companyBackImg);
        File dir = new File(RuoYiConfig.getIdentityPath()+"/"+ bizTenant.getId());
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        waterMarker.setPhysicalPath(RuoYiConfig.getIdentityPath()+"/company/"+ bizTenant.getId()+"-s.png");
        waterMarker.setRelativePath(FileUploadUtils.getRelativePath(waterMarker.getPhysicalPath()));
        List<ImgWaterMarker> imgWaterMarkers = new ArrayList<>();
        List<TextWaterMarker> textWaterMarkers = new ArrayList<>();
        ImgWaterMarker acCode = new ImgWaterMarker();
        acCode.setImgUrl(qrCodePath);
        acCode.setX(0);
        acCode.setY(50);
        acCode.setIsLocal(true);
        acCode.setIsScale(true);
        acCode.setScaleRate(1f);
        imgWaterMarkers.add(acCode);

        int widthImg = WaterMakerUtils.getImgWidth(waterMarker.getBackgroundImgUrl());

        TextWaterMarker orgName = new TextWaterMarker();
        orgName.setColor(new Color(51, 51, 51));
        orgName.setContent(StringUtils.isNotBlank(bizTenant.getOrgName())? bizTenant.getOrgName():"");
        orgName.setFont(new Font("PingFang-SC-Bold", Font.BOLD, 30));

        int widthText = WaterMakerUtils.getTextWidth(orgName.getFont(),orgName.getContent());
        System.out.println("底图宽度：" + widthImg);
        System.out.println("文本宽度：" + widthText );
        System.out.println("偏移量："+ (widthImg-widthText)/3.45);
        orgName.setX((int) ((widthImg-widthText)/3.45));
        orgName.setY(1130);
        textWaterMarkers.add(orgName);


        TextWaterMarker identity = new TextWaterMarker();
        identity.setColor(new Color(51, 51, 51));
        identity.setContent("工业标识：" + bizTenant.getId().toString());
        identity.setFont(new Font("PingFang-SC-Bold", Font.PLAIN, 30));
        widthText = WaterMakerUtils.getTextWidth(identity.getFont(),identity.getContent());
        System.out.println("底图宽度：" + widthImg);
        System.out.println("文本宽度：" + widthText );
        System.out.println("偏移量："+ (widthImg-widthText)/3.45);
        identity.setX((int) ((widthImg-widthText)/3.45));
        identity.setY(1180);
        textWaterMarkers.add(identity);
        waterMarker.setImages(imgWaterMarkers);
        waterMarker.setTexts(textWaterMarkers);
        waterMakerUtils.imageCombine(waterMarker);
        //bizTenant.setIdentityQcCode(waterMarker.getRelativePath());
    }
}
