import axios from 'axios';

// 基础配置
const baseURL = '/api'; // 根据实际情况配置API基础路径
const boardId = 1; // 默认设备ID

// 创建axios实例
const api = axios.create({
  baseURL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 响应拦截器处理响应
api.interceptors.response.use(
  response => {
    // 检查错误码
    if (response.data && response.data.error_code !== undefined) {
      if (response.data.error_code !== 0) {
        console.error('API错误:', response.data.error_code);
        return Promise.reject(response.data);
      }
    }
    return response.data;
  },
  error => {
    console.error('网络错误:', error);
    return Promise.reject(error);
  }
);

/**
 * LED双色屏控制API
 */
export const ledScreenApi = {
  /**
   * 平台上升控制
   * @returns {Promise}
   */
  platformUp() {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        platform: {
          action: "up"
        }
      }
    });
  },

  /**
   * 平台下降控制
   * @returns {Promise}
   */
  platformDown() {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        platform: {
          action: "down"
        }
      }
    });
  },

  /**
   * 屏幕开关控制
   * @param {Boolean} status - true为开，false为关
   * @returns {Promise}
   */
  screenToggle(status) {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        screen: {
          power: status ? "on" : "off"
        }
      }
    });
  },

  /**
   * 灯光模式切换
   * @param {Number} mode - 灯光模式编号
   * @returns {Promise}
   */
  lightMode(mode) {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        light: {
          mode: mode
        }
      }
    });
  },

  /**
   * 单个灯控制
   * @param {Number} lightId - 灯ID (1-6)
   * @param {Boolean} status - true为开，false为关
   * @returns {Promise}
   */
  controlSingleLight(lightId, status) {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        light: {
          id: lightId,
          status: status ? "on" : "off"
        }
      }
    });
  },

  /**
   * 调节亮度
   * @param {Number} brightness - 亮度值(0-100)
   * @returns {Promise}
   */
  setBrightness(brightness) {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        screen: {
          brightness: brightness
        }
      }
    });
  },

  /**
   * 切换节目
   * @param {Number} programId - 节目ID
   * @returns {Promise}
   */
  switchProgram(programId) {
    return api.post('/device/control', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        program: {
          id: programId
        }
      }
    });
  },

  /**
   * 设备登录验证
   * @param {String} password - 登录密码
   * @returns {Promise}
   */
  login(password) {
    return api.post('/device/auth', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        log_in: {
          type: 2,
          pass: password
        }
      }
    });
  },

  /**
   * 修改设备密码
   * @param {String} oldPassword - 旧密码
   * @param {String} newPassword - 新密码
   * @returns {Promise}
   */
  changePassword(oldPassword, newPassword) {
    return api.post('/device/auth', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        log_in: {
          type: 2,
          pass: oldPassword,
          new_pass: newPassword
        }
      }
    });
  },

  /**
   * 获取设备状态
   * @returns {Promise}
   */
  getDeviceStatus() {
    return api.post('/device/status', {
      board_id: boardId,
      JSON_id: Date.now(),
      gateway: {
        status: {
          query: "all"
        }
      }
    });
  }
};

export default ledScreenApi; 