'use strict';
const fs = require('fs');
const Log = require('ee-core/log');
const { Controller } = require('ee-core');
const  { getPhotoRecords,delectGoodsArts,createMainImage,getLastPhotoRecord } =  require('../api/takephoto');
const errData = {
  msg :'请求失败，请联系管理员',
  code:999
}
let id = 1;


/**
 * example
 * @class
 */
class takePhotoController extends Controller {

  constructor(ctx) {
    super(ctx);
  }


  /**
   * 所有方法接收两个参数
   * @param args 前端传的参数
   * @param event - ipc通信时才有值。详情见：控制器文档
   */

  /**
   * getPhotoRecords
   */
  async getPhotoRecords (params) {
    console.log('getPhotoRecords')
    try {

      console.log(id +'getPhotoRecords start print_time:'+new Date().toLocaleString())  // 打印当前时间
      const result = await getPhotoRecords(params);
      console.log(id+'getPhotoRecords result')
      console.log(id + 'getPhotoRecords result print_time:'+new Date().toLocaleString())  // 打印当前时间
      id++;
      if(result.data)  return result.data
      return errData;
    } catch (error) {
      Log.error('获取照片记录失败:', error);
      return errData;
    }
  }

  async delectGoodsArts (params) {
    console.log('delectGoodsArts')
    try {
      const result = await delectGoodsArts(params);
      console.log('result')
      console.log(result)
      if(result.data)  return result.data
      return errData;
    } catch (error) {
      console.log('error')
      Log.error('删除商品货号失败:', error);
      return errData;
    }
  }
  async createMainImage (params) {
    console.log('createMainImage')
    try {
      const result = await createMainImage(params);
      console.log('result')
      console.log(result)
      if(result.code === 0 && result.data?.main_out_path){
        const filePath =  result.data?.main_out_path
        const fileBuffer = fs.readFileSync(filePath);
        const base64Image = fileBuffer.toString('base64');


        // 获取文件扩展名
        const extension = path.extname(filePath).toLowerCase().replace('.', '');

        // 根据扩展名确定 MIME 类型
        let mimeType = '';
        switch (extension) {
          case 'jpg':
          case 'jpeg':
            mimeType = 'image/jpeg';
            break;
          case 'png':
            mimeType = 'image/png';
            break;
          case 'gif':
            mimeType = 'image/gif';
            break;
          case 'webp':
            mimeType = 'image/webp';
            break;
          case 'avif':
            mimeType = 'image/avif';
            break;
          default:
            mimeType = 'application/octet-stream'; // 默认 MIME 类型
            break;
        }

        // 构建 data URL
        const dataUrl = `data:${mimeType};base64,${base64Image}`;

        result.data.main_out_path = dataUrl

      }
      if(result.data)  return result.data
      return errData;
    } catch (error) {
      Log.error('创建主图失败:', error);
      return errData;
    }
  }

  async getLastPhotoRecord (params) {
    console.log('getLastPhotoRecord')
    try {
      const result = await getLastPhotoRecord(params);
      console.log('result')
      console.log(result)
      if(result.data)  return result.data
      return errData;
    } catch (error) {
      Log.error('获取最新照片记录失败:', error);
      return errData;
    }
  }


}

takePhotoController.toString = () => '[class takePhotoController]';
module.exports = takePhotoController;
