<script setup lang="ts">
import { native_api, openBridge } from '@/utils'

import appList from './app.json'
import android from './android.json'

const showTool = ref(false)
const params = ref({})
const input = ref('')

function toolClick(item: anyObj) {
  try {
    let res
    if (item.key) {
      res = native_api[item.method]?.(item.key, item.value)
    } else if (item.value === -1) {
      res = native_api[item.method]?.(input.value)
    } else if (item.value) {
      res = native_api[item.method]?.(item.value)
    } else {
      res = native_api[item.method]?.()
    }
    params.value = res
  } catch (err) {
    console.log(err)
    params.value = JSON.stringify(err)
  }
}
function onOpen(app: anyObj, config: anyObj) {
  params.value = {
    pkg: config.pkg || app.pkg,
    cls: config.cls || app.cls,
    flags: config.flags || app.flags,
    extra: config?.extra,
  }
  openBridge('app', JSON.stringify(params.value))
}
</script>

<template>
  <div class="x-a-tool" @click="showTool = !showTool">
    <Icon name="add_book" size="80" />
  </div>
  <XLDia v-model="showTool" class="x-a-tool-dia">
    <div class="text">
      {{ params }}
    </div>
    <el-scrollbar>
      <el-collapse>
        <el-collapse-item title="安卓方法" name="安卓方法">
          <el-button
            v-for="item in android.list"
            :key="item.label"
            color="#626aef"
            @click="toolClick(item)"
          >
            {{ item.label }}
          </el-button>
        </el-collapse-item>
        <el-collapse-item
          v-for="item in appList.list"
          :key="item.app"
          :title="item.app"
          :name="item.app"
        >
          <el-button
            v-for="btn in item.buttons"
            :key="btn.label"
            color="#626aef"
            @click="onOpen(item, btn)"
          >
            {{ btn.label }}
          </el-button>
        </el-collapse-item>
      </el-collapse>
    </el-scrollbar>
    <!-- <div v-html="info"></div>
        <el-input v-model="input"></el-input>
        <Btn
            v-for="item in tools"
            :key="item.label"
            width="170"
            size="26"
            @click="toolClick(item)"
            >{{ item.label }}</Btn
        > -->
  </XLDia>
</template>

<style lang="scss" scoped>
.x-a-tool {
  width: 100px;
  height: 8.33vh;
  background: #fff;
  border-radius: 50%;
  position: fixed;
  bottom: 10vh;
  left: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.x-a-tool-dia {
  .text {
    padding: $space-1;
    text-align: left;
    white-space: normal;
    overflow-wrap: break-all;
    word-break: break-all;
    user-select: auto;
    background: rgba(0, 0, 0, 0.8);
    color: #24c238;
  }
  .el-button {
    height: 5vh;
    font-size: 1.83vh;
    margin-bottom: $space-1_h;
  }

  .el-scrollbar {
    max-height: 53.33vh;
  }

  .el-collapse {
    background: transparent;
    padding: 0 $space-4;
    border: none;
    .el-collapse-item {
      .el-collapse-item__header {
        height: 6.67vh;
        font-size: $font-size-5;
        line-height: 1.5em;
        font-weight: 400;
        color: $font-tag-color;
        border: none;
        position: relative;
        justify-content: space-between;
        background: transparent;
      }
      .el-collapse-item__wrap {
        background: transparent;
      }
    }
  }
}
</style>
