package com.iotechn.unimall.admin;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Hashtable;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.iotechn.unimall.core.util.GeneratorUtil;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class QRCodeGenerator implements InitializingBean {
    @Value("${oss.aliyun.oss.basekUrl}")
    private String baseUrl;
    @Value("${oss.aliyun.oss.bucket}")
    private String bucket;
    @Autowired
    private OSSClient ossClient;
    @Value("${oss.aliyun.oss.endpoint}")
    private String endpoint;

    private String host;
    @Override
    public void afterPropertiesSet() throws Exception {
        host = "http://" + bucket + "." + endpoint;
    }
    public  String generateQRCodeImage(String text, int width, int height,String sendCarNo) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Hashtable<EncodeHintType, Object> hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);

        File file = new File( new String(("templates" + File.separator + sendCarNo+".png").getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
        Path path = FileSystems.getDefault().getPath(file.getAbsoluteFile().getPath());

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

        FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());

        MultipartFile multipartFile = new MockMultipartFile( ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        return upload(multipartFile,sendCarNo);
    }
    /**
     * 后台通过服务器间接传文件
     * @param file
     * @return
     * @throws IOException
     */
    public  String upload(MultipartFile file,String sendCarNo) throws IOException {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.getSize());
        objectMetadata.setContentType(file.getContentType());
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, "QRCode/" +sendCarNo+".png", file.getInputStream(), objectMetadata);
        ossClient.putObject(putObjectRequest);
        return baseUrl + "QRCode/" + sendCarNo+".png";
    }

}
