'use strict';
const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const { checkCameraControlCmdExists, closeCameraControlTips} = require('../utils/camera');
const {
  checkCamera,
  liveShow, liveHide, setParams, capture, getParams,CMD,captureLive,closeOtherWindow } = require('../api/camera');

const { readConfigFile } = require('../utils/config');

let  isOPen = true
class CameraController extends Controller {
  constructor(ctx) {
    super(ctx);
  }

  async connect() {
    try {
      console.log('==================');
      console.log(readConfigFile());
      if(readConfigFile().controlType === 'digiCamControl'){

        console.log('========1==========');
        await getParams('iso').catch(e=>{
          isOPen = false;
        })
        if(!isOPen){
          await checkCameraControlCmdExists()
          await  CMD('All_Minimize')
          await closeCameraControlTips()
          isOPen = true
        }
        const res = await getParams('iso')
        if(res  === '未将对象引用设置到对象的实例。'){
          return {
            status:-1,
            msg:"相机未连接，请链接相机。",
          }
        }
        return {
          status:2,
          msg:res,
        }


      }else{
        let res = await checkCamera().catch(e=>{
          isOPen = false;
        })

        if(res?.device_status  === -1 ){
          isOPen = false;
          await checkCameraControlCmdExists()
          isOPen = true
          await new Promise(resolve => setTimeout(resolve, 10000)); // 等待5秒
          res = await checkCamera()
        }

        if(res?.device_status  === 2){
          isOPen = true;
          return  {
            ...res,
            status:2
          };
        }


        return {
          status:-1,
          msg:"相机未连接，请链接相机。",
        }


      }


    } catch (error) {

      let msg = '请安装digiCamControl软件，并打开digiCamControl软件的web服务，端口为5513'
      if(readConfigFile().controlType === 'SmartShooter') msg = '请安装SmartShooter5软件'
      return {
        status:-1,
        msg,
      }
    }
  }

  /**
   * 启动预览
   */
  async liveShow() {
    try {
      await liveShow();
      if(readConfigFile().controlType === 'digiCamControl'){
        await  CMD('All_Minimize')
      }
      return true;
    } catch (error) {
      console.error('eeee启动直播失败:', error);
      throw error;
    }
  }

  /**
   * 结束预览
   */
  async liveHide() {
    try {
      await liveHide();
      return true;
    } catch (error) {
      throw error;
    }
  }

  /**
   * 设置参数
   */
  async setParams(params) {
    try {
      console.log(params);
      await setParams(params);
      return true;
    } catch (error) {
      throw error;
    }
  }

  async takePictures(isLive=true) {
    try {
      if(isLive){

        await captureLive();
      }else{

        await capture();
      }
      return true;
    } catch (error) {
      throw error;
    }
  }

  // 示例：使用 fetchData 函数进行 HTTP 请求
  async fetchExampleData() {
    const options = {
      hostname: 'example.com',
      port: 443,
      path: '/api/data',
      method: 'GET'
    };

    try {
      const data = await fetchData(options);
      console.log('Fetched data:', data);
      return data;
    } catch (error) {
      console.error('Fetch data failed:', error);
      throw error;
    }
  }
}

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