
const Log = require('ee-core/log');
const CoreWindow = require('ee-core/electron/window');
const WebSocket = require('ws'); // 引入原生 ws 库
const { readConfigFile } = require('./config');
const pyapp = readConfigFile().pyapp
const { app } = require('electron');
const path = require('path');
const fs = require('fs');

const typeToMessage = {
  run_mcu_single:['seeting','default'],
  get_deviation_data:"developer",
  set_deviation:"developer",
  get_mcu_other_info:"developer",
  set_mcu_other_info:"developer",
  send_command:"developer",
  smart_shooter_get_camera_property:"seeting",
  detail_progress:"PhotographyDetail",
  segment_progress:"PhotographyDetail",
  upper_footer_progress:"PhotographyDetail",
  scene_progress:"PhotographyDetail",
  upload_goods_progress:"PhotographyDetail",
  detail_result_progress:"PhotographyDetail"
}


const previewPath = path.join(app.getPath("userData"),'preview','liveview.png');

// 确保目录存在的函数
function ensureDirectoryExistence(filePath) {
  const dir = path.dirname(filePath);
  if (fs.existsSync(dir)) {
    return true;
  }
  ensureDirectoryExistence(path.dirname(dir));
  fs.mkdirSync(dir);
}

function livePreview(data){
  if(data.msg === '预览数据发送' && data.code === 1){

    ensureDirectoryExistence(previewPath);
    const tempFilePath = `${previewPath}.tmp`;

    fs.writeFile(tempFilePath, data.data.smart_shooter_preview, 'base64', (err) => {
      if (err) {
        Log.error('写入临时文件失败:', err);
      } else {
        fs.rename(tempFilePath, previewPath, (renameErr) => {
          if (renameErr) {
          } else {
          }
        });
      }
    });

 }

}

const pySocket = function () {

  this.app = null;
  // 重连配置
  this.reconnectConfig = {
    maxRetries: 5,           // 最大重连次数
    retryInterval: 3000,     // 重连间隔（毫秒）
    maxRetryInterval: 30000, // 最大重连间隔
    retryMultiplier: 1.5     // 重连间隔递增倍数
  };
  this.reconnectAttempts = 0;  // 当前重连次数
  this.isReconnecting = false; // 是否正在重连
  this.shouldReconnect = true; // 是否应该重连（用于手动断开时阻止重连）

  // 心跳配置
  this.heartbeatConfig = {
    interval: 10000,        // 心跳间隔（10秒）
    timeout: 30000,         // 心跳超时时间（30秒）
    maxMissed: 3            // 最大允许丢失的心跳次数
  };
  this.heartbeatTimer = null;     // 心跳定时器
  this.heartbeatTimeout = null;   // 心跳超时定时器
  this.missedHeartbeats = 0;      // 丢失的心跳次数
  this.lastHeartbeatTime = 0;     // 最后一次心跳时间

  // 启动心跳机制
  this.startHeartbeat = function() {
    this.stopHeartbeat(); // 先停止之前的心跳

    console.log('启动心跳机制，间隔:', this.heartbeatConfig.interval + 'ms');

    this.heartbeatTimer = setInterval(() => {
      if (app.socket && app.socket.readyState === WebSocket.OPEN) {
        this.sendPing();
        this.lastHeartbeatTime = Date.now();

        // 设置心跳超时检测
        this.setHeartbeatTimeout();
      }
    }, this.heartbeatConfig.interval);
  };

  // 停止心跳机制
  this.stopHeartbeat = function() {
    if (this.heartbeatTimer) {
      clearInterval(this.heartbeatTimer);
      this.heartbeatTimer = null;
    }
    if (this.heartbeatTimeout) {
      clearTimeout(this.heartbeatTimeout);
      this.heartbeatTimeout = null;
    }
    this.missedHeartbeats = 0;
    console.log('停止心跳机制');
  };

  // 设置心跳超时检测
  this.setHeartbeatTimeout = function() {
    if (this.heartbeatTimeout) {
      clearTimeout(this.heartbeatTimeout);
    }

    this.heartbeatTimeout = setTimeout(() => {
      this.missedHeartbeats++;
      console.log(`心跳超时，丢失次数: ${this.missedHeartbeats}/${this.heartbeatConfig.maxMissed}`);

      if (this.missedHeartbeats >= this.heartbeatConfig.maxMissed) {
        console.log('心跳超时次数过多，主动断开连接');
        if (app.socket) {
          app.socket.close();
        }
      }
    }, this.heartbeatConfig.timeout);
  };

  // 处理心跳响应
  this.handleHeartbeatResponse = function() {
    this.missedHeartbeats = 0;
    if (this.heartbeatTimeout) {
      clearTimeout(this.heartbeatTimeout);
      this.heartbeatTimeout = null;
    }
    console.log('收到心跳响应');
  };

  // 重连逻辑函数
  this.attemptReconnect = async function() {
    if (!this.shouldReconnect || this.isReconnecting) {
      return;
    }

    if (this.reconnectAttempts >= this.reconnectConfig.maxRetries) {
      Log.info('达到最大重连次数，停止重连');
      this.isReconnecting = false;
      this.reconnectAttempts = 0;
      return;
    }

    this.isReconnecting = true;
    this.reconnectAttempts++;

    // 计算重连间隔（指数退避）
    const interval = Math.min(
      this.reconnectConfig.retryInterval * Math.pow(this.reconnectConfig.retryMultiplier, this.reconnectAttempts - 1),
      this.reconnectConfig.maxRetryInterval
    );

    Log.info(`第${this.reconnectAttempts}次重连尝试，${interval}ms后开始...`);

    setTimeout(async () => {
      try {
        Log.info('开始重连...');
        await this.init();
        Log.info('重连成功');
        this.isReconnecting = false;
        this.reconnectAttempts = 0;
      } catch (error) {
        Log.info('重连失败:', error);
        this.isReconnecting = false;
        // 继续尝试重连
        this.attemptReconnect();
      }
    }, interval);
  };

  this.init = async function (this_app) {
    if(this_app)   this.app = this_app;
    await new Promise(async (resolve,reject) => {

      const win = CoreWindow.getMainWindow()
      if(app.socket){
        resolve(true);
        win.webContents.send('controller.socket.connect_open', true);
        return;
      }

      // 重置重连状态
      this.shouldReconnect = true;
      this.isReconnecting = false;

      app.socket = new WebSocket('ws://'+pyapp+':7074/ws');

      // 监听连接成功事件
      app.socket.on('open', () => {
        Log.info('socket open')
        resolve(true);
        win.webContents.send('controller.socket.connect_open', true);

        // 启动心跳机制
        this.startHeartbeat();
      });

      // 监听消息事件
      app.socket.on('message', (data) => {
        try {
          let this_data = JSON.parse(data.toString());

          if(!['blue_tooth','smart_shooter_enable_preview','smart_shooter_getinfo'].includes(this_data.msg_type)){
            console.log('message');
            console.log(this_data);
          //  Log.info(this_data);
          }
          if(this_data.msg_type){
            let notAllMessage = false
            switch (this_data.msg_type){
              case 'smart_shooter_enable_preview':
                notAllMessage = true;
                livePreview(this_data);
                break;
              case 'pong':
                // 处理心跳响应
                this.handleHeartbeatResponse();
                notAllMessage = true;
                break;
            }
            if(notAllMessage) return;
            let channel = 'controller.socket.message_'+this_data.msg_type;
            if(typeToMessage[this_data.msg_type]){
              if(typeof typeToMessage[this_data.msg_type] === 'object'){

                typeToMessage[this_data.msg_type].map(item=>{
                  if(item === 'default'){
                    win.webContents.send(channel, this_data);
                  }else{
                    if(this.app.electron[item]) {
                      this.app.electron[item].webContents.send(channel, this_data);
                    }
                  }
                })
              }else{
                if(this.app.electron[typeToMessage[this_data.msg_type]]) {
                  this.app.electron[typeToMessage[this_data.msg_type]].webContents.send(channel, this_data);
                }
              }
            }else{
              win.webContents.send(channel, this_data);
            }
          }
        }catch (e){
          console.log(e)
        }
      });

      // 监听连接关闭事件
      app.socket.on('close', (e) => {
        Log.info('socket close');
        Log.info(e);
        win.webContents.send('controller.socket.disconnect', null);

        // 停止心跳机制
        this.stopHeartbeat();

        app.socket = null;

        // 启动重连机制
        this.attemptReconnect();
      });

      // 监听错误事件
      app.socket.on('error', (err) => {
        Log.info('socket error:', err);
        win.webContents.send('controller.socket.disconnect', null);

        // 停止心跳机制
        this.stopHeartbeat();

        app.socket = null;

        // 启动重连机制
        this.attemptReconnect();
        reject(true);
      });


    })

  }
  this.sendPing = function () {
    const message = JSON.stringify({ data: 'node', type: 'ping' });
    this.sendMessage(message);
  }
  this.sendMessage = async function (message) {
    console.log('socket.=========================sendMessage');
    console.log('socket.sendMessage');
    console.log(message);
    console.log(app.socket?.readyState);
    if(!app.socket){
      await  this.init()
    }
    // 检查连接状态
    if (app.socket?.readyState === WebSocket.OPEN) {
      console.log('send');
      app.socket.send(message); // 使用 send() 发送
    }
  }

  this.disconnect = function () {
    // 设置标志，阻止重连
    this.shouldReconnect = false;
    this.isReconnecting = false;
    this.reconnectAttempts = 0;

    // 停止心跳机制
    this.stopHeartbeat();

    if (app.socket) {
      app.socket.close(); // 使用 close() 方法
      app.socket = null;
    }
  }

  // 重新启用重连功能
  this.enableReconnect = function () {
    this.shouldReconnect = true;
    this.reconnectAttempts = 0;
    this.isReconnecting = false;
  }
  this.onSocketMessage = async function (message_type,callback) {  // 监听消息事件
    return new Promise(async (resolve,reject) => {
      app.socket.on('message', onSocketMessage);
      async function onSocketMessage(data){
          try {
            let this_data = JSON.parse(data.toString());
            if(this_data.msg_type === message_type){
              app.socket.off('message', onSocketMessage);
              callback(this_data)
              resolve()
            }
          }catch (e){
            Log.error(e)
            reject(e)
          }
      }
    })
  }
  return this;
}


module.exports = pySocket;
