package com.inspect.infrared.controller;

import com.inspect.base.core.exception.ServiceException;
import com.inspect.base.core.utils.ByteUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.infrared.InfraredImageParser;
import com.inspect.infrared.InfraredRequestParam;
import com.inspect.infrared.InfraredResponse;
import com.inspect.infrared.InfraredT;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/infrared"})
public class InfraredController extends BaseController {
    @Autowired
    private InfraredImageParser infraredImageParser;

    @GetMapping({"test"})
    public AjaxResult test() throws Exception {
        InfraredResponse parse;
        try {
            InfraredRequestParam infraredRequestParam = new InfraredRequestParam();
            infraredRequestParam.setRequestId("1");
            List<InfraredRequestParam.Param> list = new ArrayList<>();
            InfraredRequestParam.Param param = new InfraredRequestParam.Param();
            List<String> imageList = new ArrayList<>();
            imageList.add("1/2022/06/09/TASK_01/FIR/POINT_01_DEVICE_01_20220609154844.jpg");
            param.setImageUrlList(imageList);
            param.setObjectId("1");
            list.add(param);
            infraredRequestParam.setObjectList(list);
            parse = this.infraredImageParser.parse(infraredRequestParam);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("测温失败！");
        }

        return AjaxResult.success(parse);
    }

    @GetMapping
    public AjaxResult parse(String imgUrl, String coordinate) throws Exception {
        InfraredT parse;
        try {
            parse = this.infraredImageParser.parse(imgUrl, coordinate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("测温失败！");
        }

        return AjaxResult.success(parse);
    }

    @GetMapping({"combine"})
    public AjaxResult combine(String fileName) throws Exception {
        String packagePath = "E:/";
        String fileSuf = ".jpg";
        String irPro = "_ir.jpg";
        int width = 640;
        int height = 480;
        String filePath = packagePath + fileName + fileSuf;
        FileInputStream fileInputStream = new FileInputStream(filePath);
        byte[] bytes = new byte[1024];
        ByteBuf byteBuf = Unpooled.buffer();

        int size;
        while ((size = fileInputStream.read(bytes)) > -1) {
            byteBuf.writeBytes(Arrays.copyOfRange(bytes, 0, size));
        }

        int start = byteBuf.readableBytes();
        fileInputStream.close();
        byteBuf.writeBytes(new byte[2]);
        byteBuf.writeBytes(ByteUtil.getBytes((short) width));
        byteBuf.writeBytes(ByteUtil.getBytes((short) height));
        byteBuf.writeBytes(new byte[14]);
        float[] random = new float[]{50.0F, 51.0F, 52.0F, 53.0F, 54.0F, 55.0F, 56.0F, 57.0F, 58.0F, 59.0F};

        for (int i = 0; i < width * height; ++i) {
            byteBuf.writeBytes(ByteUtil.getBytes(random[i % 10]));
        }

        byteBuf.writeBytes(ByteUtil.getBytes(0.9F));
        byteBuf.writeBytes(ByteUtil.getBytes(20.9F));
        byteBuf.writeBytes(new byte[1]);
        byteBuf.writeBytes(ByteUtil.getBytes(5));
        byteBuf.writeBytes(new byte[1]);
        byteBuf.writeBytes(new byte[4]);
        byteBuf.writeBytes(new byte[32]);
        byteBuf.writeBytes(new byte[32]);
        byteBuf.writeBytes(new byte[32]);
        byteBuf.writeBytes(new byte[8]);
        byteBuf.writeBytes(new byte[8]);
        byteBuf.writeBytes(new byte[4]);
        String remark = "备注信息";
        byte[] remarkBytes = remark.getBytes();
        byteBuf.writeBytes(ByteUtil.getBytes(remarkBytes.length));
        byteBuf.writeBytes(remarkBytes);
        byteBuf.writeBytes(ByteUtil.getBytes(start));
        byteBuf.writeBytes(new byte[16]);
        byte[] total = new byte[byteBuf.readableBytes()];
        byteBuf.getBytes(0, total);
        FileOutputStream fileOutputStream = new FileOutputStream(filePath.replace(fileSuf, irPro));
        fileOutputStream.write(total);
        fileOutputStream.close();
        return AjaxResult.success();
    }
}
