const axios = require('axios');
const http = require('http');
const { net } = require('electron');

//
const baseURL = 'http://localhost:5513/';
// 创建 Axios 实例
const service = axios.create({
  baseURL:baseURL,
  timeout: 60000,
});


// 封装 GET 方法
function get(config = { url: '' }) {
  return service.get(config.url, {
    insecureHTTPParser: true,
    timeout: config.timeout || 60000,
  });
}


async function fetchExampleData(url) {
  return new Promise((resolve, reject) => {
    const request = net.request({
      method: 'GET',
      url: baseURL+url
    });

    request.on('response', (response) => {
      let data = '';
      response.on('data', (chunk) => {
        data += chunk;
      });
      response.on('end', () => {
        try {
          resolve(data);
        } catch (error) {
          console.error('Error parsing data:', error);
          reject(error);
        }
      });
    });

    request.on('error', (error) => {
      console.error('Request error:', error);
      reject(error);
    });

    request.end();
  });
}



module.exports = {
  liveShow(){
    return get({
      url: '?CMD=LiveViewWnd_Show'
    })
  },
  liveHide(){
    return get({
      url: '?CMD=LiveViewWnd_Hide'
    })
  },
  capture(){
    return get({
      url: '?CMD=LiveView_Capture'
    })
  },

  CMD(cmd){
    return get({
      url: '?CMD='+cmd
    })
  },

  getParams(params){
    return  fetchExampleData(`?slc=get&param1=${params}`)
  },
  setParams(params){

    return get({
      url: `?slc=set&param1=${params.key}&param2=${params.value}`
    })
  },
}


/*
*
* 设置   iso
* 光圈  aperture
* 拍摄模式  mode
* 快门速度  shutterSpeed
* 白平衡    whitebalance
* 曝光补偿  ExposureCompensation
* 对焦模式 focusmode
*
*folder 文件夹
曝光度：exposure
压缩：compression
测光点：metering
* */
