<template>
  <view class="container">
    <view class="header">
      <text class="title">SMDT设备管理插件示例</text>
      <view class="status-indicator">
        <text class="status-text" :class="{ 'status-ready': isPluginReady, 'status-error': !isPluginReady }">
          {{ isPluginReady ? '✅ 插件已就绪' : '❌ 插件未就绪' }}
        </text>
      </view>
    </view>
    
    <!-- 系统功能 -->
    <view class="section">
      <text class="section-title">系统功能</text>
      
      <view class="button-group">
        <button @click="getSystemFontSize" class="btn">获取系统字体大小</button>
        <button @click="setSystemFontSize" class="btn">设置系统字体大小</button>
        <button @click="getTimeFormat" class="btn">获取时间格式</button>
        <button @click="setTimeFormat" class="btn">设置时间格式</button>
        <button @click="setVolume" class="btn">设置音量</button>
        <button @click="setAdbDebug" class="btn">设置ADB调试</button>
      </view>
    </view>
    
    <!-- 网络功能 -->
    <view class="section">
      <text class="section-title">网络功能</text>
      
      <view class="button-group">
        <button @click="getWifiRssi" class="btn">获取WiFi信号强度</button>
        <button @click="getMacAddress" class="btn">获取MAC地址</button>
        <button @click="getImeiNumber" class="btn">获取IMEI号</button>
        <button @click="setWifiAp" class="btn">设置WiFi热点</button>
      </view>
    </view>
    
    <!-- 显示功能 -->
    <view class="section">
      <text class="section-title">显示功能</text>
      
      <view class="button-group">
        <button @click="setLcdBackLight" class="btn">设置屏幕亮度</button>
        <button @click="setDisplayDensity" class="btn">设置显示密度</button>
      </view>
    </view>
    
    <!-- 硬件功能 -->
    <view class="section">
      <text class="section-title">硬件功能</text>
      
      <view class="button-group">
        <button @click="setLedLighted" class="btn">控制LED灯</button>
        <button @click="getLedState" class="btn">获取LED状态</button>
        <button @click="getSDcardPath" class="btn">获取SD卡路径</button>
        <button @click="getUdiskPath" class="btn">获取U盘路径</button>
        <button @click="setUsbPower" class="btn">控制USB电源</button>
      </view>
    </view>
    
    <!-- 结果显示 -->
    <view class="result-section">
      <text class="result-title">操作结果:</text>
      <text class="result-text">{{ result }}</text>
    </view>
  </view>
</template>

<script>
// 导入插件管理器和API
import { PluginManager, SystemAPI, NetworkAPI, DisplayAPI, HardwareAPI } from './smdtManager.js';

export default {
  data() {
    return {
       result: '等待操作...',
       isPluginReady: false,
	   smdtManager:null
    }
  },
  
  async onLoad() {
    this.result = '正在初始化插件...';
    try {
		
			this.smdtManager = uni.requireNativePlugin('SmdtManager');
			this.smdtManager.initialize((res) => {
				  if(res.success){
					  this.isPluginReady = true;
					  this.result = '插件初始化成功，可以开始使用功能';
					  uni.showToast({
						title: '插件初始化成功',
						icon: 'success'
					  });
				  }
			});	
			
	         // await PluginManager.initialize();
	         // this.isPluginReady = true;
	         // this.result = '插件初始化成功，可以开始使用功能';
	  
    } catch (error) {
		  this.isPluginReady = false;
		  this.result = `插件初始化失败: ${error.message}`;
		  // 显示用户友好的错误提示
		  uni.showModal({
			 title: '插件初始化失败',
			 content: '设备功能暂不可用，请检查设备兼容性或联系技术支持',
			 showCancel: false
		  });
    }
  },
  
  methods: {
    // 通用错误处理方法
    handleError(error, operation) {
      console.error(`${operation}失败:`, error);
      this.result = `${operation}失败: ${error.message}`;
      
      if (error.message.includes('初始化失败')) {
        uni.showModal({
          title: '设备兼容性问题',
          content: '当前设备可能不支持此功能，请联系技术支持',
          showCancel: false
        });
      }
    },

    // 系统功能方法
    async getSystemFontSize() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const fontSize = await SystemAPI.getSystemFontSize();
        this.result = `系统字体大小: ${fontSize}`;
      } catch (error) {
        this.handleError(error, '获取系统字体大小');
      }
    },
    
    async setSystemFontSize() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await SystemAPI.setSystemFontSize(1.2);
        this.result = `设置字体大小结果: ${result}`;
        uni.showToast({
          title: '字体大小设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置系统字体大小');
      }
    },
    
    async getTimeFormat() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const timeFormat = await SystemAPI.getTimeFormat();
        this.result = `时间格式: ${timeFormat === 1 ? '24小时制' : '12小时制'}`;
      } catch (error) {
        this.handleError(error, '获取时间格式');
      }
    },
    
    async setTimeFormat() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await SystemAPI.setTimeFormat(1); // 1为24小时制，0为12小时制
        this.result = `设置时间格式结果: ${result}`;
        uni.showToast({
          title: '时间格式设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置时间格式');
      }
    },
    
    async setVolume() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await SystemAPI.setVolume(50); // 音量范围通常是0-100
        this.result = `设置音量结果: ${result}`;
        uni.showToast({
          title: '音量设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置音量');
      }
    },
    
    async setAdbDebug() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await SystemAPI.setAdbDebug(true);
        this.result = `设置ADB调试结果: ${result}`;
        uni.showToast({
          title: 'ADB调试设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置ADB调试');
      }
    },
    
    // 网络功能方法
    async getWifiRssi() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const rssi = await NetworkAPI.getWifiRssi();
        this.result = `WiFi信号强度: ${rssi} dBm`;
      } catch (error) {
        this.handleError(error, '获取WiFi信号强度');
      }
    },
    
    async getMacAddress() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const macAddress = await NetworkAPI.getMacAddress();
        this.result = `MAC地址: ${macAddress}`;
      } catch (error) {
        this.handleError(error, '获取MAC地址');
      }
    },
    
    async getImeiNumber() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const imei = await NetworkAPI.getImeiNumber();
        this.result = `IMEI号: ${imei}`;
      } catch (error) {
        this.handleError(error, '获取IMEI号');
      }
    },
    
    async setWifiAp() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await NetworkAPI.setWifiAp('SMDT_AP', '12345678');
        this.result = `设置WiFi热点结果: ${result}`;
        uni.showToast({
          title: 'WiFi热点设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置WiFi热点');
      }
    },
    
    // 显示功能方法
    async setLcdBackLight() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await DisplayAPI.setLcdBackLight(128); // 亮度范围通常是0-255
        this.result = `设置屏幕亮度结果: ${result}`;
        uni.showToast({
          title: '屏幕亮度设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置屏幕亮度');
      }
    },
    
    async setDisplayDensity() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await DisplayAPI.setDisplayDensity(320); // DPI值
        this.result = `设置显示密度结果: ${result}`;
        uni.showToast({
          title: '显示密度设置成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '设置显示密度');
      }
    },
    
    // 硬件功能方法
    async setLedLighted() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await HardwareAPI.setLedLighted(1, true);
        this.result = `控制LED灯结果: ${result}`;
        uni.showToast({
          title: 'LED灯控制成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '控制LED灯');
      }
    },
    
    async getLedState() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const state = await HardwareAPI.getLedState(1);
        this.result = `LED状态: ${state ? '开启' : '关闭'}`;
      } catch (error) {
        this.handleError(error, '获取LED状态');
      }
    },
    
    async getSDcardPath() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const path = await HardwareAPI.getSDcardPath();
        this.result = `SD卡路径: ${path}`;
      } catch (error) {
        this.handleError(error, '获取SD卡路径');
      }
    },
    
    async getUdiskPath() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const path = await HardwareAPI.getUdiskPath();
        this.result = `U盘路径: ${path}`;
      } catch (error) {
        this.handleError(error, '获取U盘路径');
      }
    },
    
    async setUsbPower() {
      if (!this.isPluginReady) {
        this.result = '插件未就绪，请等待初始化完成';
        return;
      }
      
      try {
        const result = await HardwareAPI.setUsbPower(true);
        this.result = `控制USB电源结果: ${result}`;
        uni.showToast({
          title: 'USB电源控制成功',
          icon: 'success'
        });
      } catch (error) {
        this.handleError(error, '控制USB电源');
      }
    }
  }
}
</script>

<style>
.container {
  padding: 20px;
  background-color: #f5f5f5;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.status-indicator {
  margin-top: 10px;
}

.status-text {
  font-size: 14px;
  padding: 5px 10px;
  border-radius: 15px;
  background-color: #f0f0f0;
}

.status-ready {
  color: #27ae60;
  background-color: #d5f4e6;
}

.status-error {
  color: #e74c3c;
  background-color: #fdeaea;
}

.section {
  margin-bottom: 30px;
  background-color: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 15px;
  display: block;
}

.button-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn {
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 12px 20px;
  font-size: 16px;
  margin: 5px 0;
}

.btn:active {
  background-color: #2980b9;
}

.result-section {
  background-color: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.result-title {
  font-size: 16px;
  font-weight: bold;
  color: #2c3e50;
  display: block;
  margin-bottom: 10px;
}

.result-text {
  font-size: 14px;
  color: #34495e;
  line-height: 1.5;
  word-wrap: break-word;
}
</style>