<script setup>
import PlayIcon from '@vicons/ionicons5/Play'
import { inject } from 'vue'

const running = inject('running')
const log = inject('log')
const steps = inject('steps')
const current_step = inject('current_step')
const current_state = inject('current_state')

const notification = useNotification()

async function start() {
  log.value = ''
  running.value = true
  for (const [i, step] of steps.value.entries()) {
    current_step.value = i + 1
    current_state.value = 'process'
    for (const cmd of step.command) {
      if ((await pywebview.api.run(cmd, step.cwd)) == 'failed') {
        current_state.value = 'error'
        running.value = false
        notification['error']({
          content: '错误',
          meta: '命令运行失败',
          duration: 3000
        })
        return
      }
    }
  }
  current_state.value = 'finish'
  running.value = false
  notification['info']({
    content: '提示',
    meta: '命令运行完成',
    duration: 3000
  })
}
</script>

<template>
  <n-button
    class="float"
    type="primary"
    :loading="running"
    :disabled="running"
    @click="start"
    circle
    size="large"
  >
    <template #icon>
      <n-icon>
        <play-icon />
      </n-icon>
    </template>
  </n-button>
</template>

<style scoped>
.float {
  position: absolute;
  right: 36px;
  bottom: 36px;
  opacity: 0.8;
}

.float:hover {
  opacity: 1;
}
</style>
