<script setup lang="ts">
import type {
  AdminCloudtentaclesLoginTestResult,
  AdminCloudtentaclesPersistedSession,
  AdminCloudtentaclesSendSmsResult,
  AdminCloudtentaclesSessionsMap,
  AdminCloudtentaclesSourceItem,
  AdminCloudtentaclesValidateSessionResult,
} from '@/types/admin'
import AdminCloudtentaclesInfoCard from './AdminCloudtentaclesInfoCard.vue'
import AdminCloudtentaclesConfigCard from './AdminCloudtentaclesConfigCard.vue'
import AdminCloudtentaclesDebugCard from './AdminCloudtentaclesDebugCard.vue'
import AdminCloudtentaclesResultCard from './AdminCloudtentaclesResultCard.vue'

type CloudtentaclesForm = {
  baseUrl: string
  username: string
  password: string
  phone: string
  code: string
  token: string
  vnKey: string
  vnId: number
  skuId: number
  skuCount: number
  deviceId: string
  deviceType: number
}

type SourceOption = {
  key: string
  label: string
}

type CloudtentaclesStats = {
  hasCredential: boolean
  smsSent: boolean
  validated: boolean
  sourceCount: number
}

type Props = {
  cloudtentaclesFilePath: string
  cloudtentaclesSessionFilePath: string
  cloudtentaclesForm: CloudtentaclesForm
  cloudtentaclesSaving: boolean
  cloudtentaclesSendingSms: boolean
  cloudtentaclesTesting: boolean
  cloudtentaclesValidating: boolean
  cloudtentaclesResultError: string
  cloudtentaclesSmsResult: AdminCloudtentaclesSendSmsResult | null
  cloudtentaclesLoginResult: AdminCloudtentaclesLoginTestResult | null
  cloudtentaclesValidateResult: AdminCloudtentaclesValidateSessionResult | null
  cloudtentaclesDebugLoading: boolean
  cloudtentaclesDebugResult: string
  cloudtentaclesPersistedSession: AdminCloudtentaclesPersistedSession | null
  cloudtentaclesStats: CloudtentaclesStats
  sources: AdminCloudtentaclesSourceItem[]
  sessions: AdminCloudtentaclesSessionsMap
  selectedSourceKey: string
  sourceOptions: SourceOption[]
  handleCloudtentaclesSaveSource: () => void | Promise<void>
  handleCloudtentaclesSendSmsCode: () => void | Promise<void>
  handleCloudtentaclesTestLogin: () => void | Promise<void>
  handleCloudtentaclesValidateSession: () => void | Promise<void>
  handleCloudtentaclesFetchAsset: () => void | Promise<void>
  handleCloudtentaclesFetchCategories: () => void | Promise<void>
  handleCloudtentaclesFetchSkuList: () => void | Promise<void>
  handleCloudtentaclesBuySku: () => void | Promise<void>
  handleCloudtentaclesUseSku: () => void | Promise<void>
  handleCloudtentaclesFetchKnapsack: () => void | Promise<void>
  handleCloudtentaclesFetchVnList: () => void | Promise<void>
  handleCloudtentaclesAppointVn: () => void | Promise<void>
  handleCloudtentaclesGenerateVnLoginCode: () => void | Promise<void>
  handleCloudtentaclesFetchVnCode: () => void | Promise<void>
  handleCloudtentaclesVerifyVnCode: () => void | Promise<void>
  handleCloudtentaclesFetchBindUrl: () => void | Promise<void>
  handleCloudtentaclesBackVn: () => void | Promise<void>
  handleCloudtentaclesRunFullFlow: () => void | Promise<void>
  switchSource: (sourceKey: string) => void
  addSource: () => void
  removeSource: (sourceKey: string) => void
}

defineProps<Props>()
</script>

<template>
  <div class="platform-section">
    <AdminCloudtentaclesInfoCard
      :cloudtentacles-file-path="cloudtentaclesFilePath"
      :cloudtentacles-session-file-path="cloudtentaclesSessionFilePath"
      :cloudtentacles-persisted-session="cloudtentaclesPersistedSession"
      :sources="sources"
      :sessions="sessions"
      :selected-source-key="selectedSourceKey"
    />

    <AdminCloudtentaclesConfigCard
      :cloudtentacles-form="cloudtentaclesForm"
      :cloudtentacles-saving="cloudtentaclesSaving"
      :cloudtentacles-sending-sms="cloudtentaclesSendingSms"
      :cloudtentacles-testing="cloudtentaclesTesting"
      :cloudtentacles-validating="cloudtentaclesValidating"
      :cloudtentacles-result-error="cloudtentaclesResultError"
      :cloudtentacles-sms-result="cloudtentaclesSmsResult"
      :cloudtentacles-login-result="cloudtentaclesLoginResult"
      :cloudtentacles-validate-result="cloudtentaclesValidateResult"
      :selected-source-key="selectedSourceKey"
      :source-options="sourceOptions"
      :sources="sources"
      :handle-cloudtentacles-save-source="handleCloudtentaclesSaveSource"
      :handle-cloudtentacles-send-sms-code="handleCloudtentaclesSendSmsCode"
      :handle-cloudtentacles-test-login="handleCloudtentaclesTestLogin"
      :handle-cloudtentacles-validate-session="handleCloudtentaclesValidateSession"
      :switch-source="switchSource"
      :add-source="addSource"
      :remove-source="removeSource"
    />

    <AdminCloudtentaclesDebugCard
      :cloudtentacles-debug-loading="cloudtentaclesDebugLoading"
      :cloudtentacles-debug-result="cloudtentaclesDebugResult"
      :handle-cloudtentacles-fetch-asset="handleCloudtentaclesFetchAsset"
      :handle-cloudtentacles-fetch-categories="handleCloudtentaclesFetchCategories"
      :handle-cloudtentacles-fetch-sku-list="handleCloudtentaclesFetchSkuList"
      :handle-cloudtentacles-buy-sku="handleCloudtentaclesBuySku"
      :handle-cloudtentacles-use-sku="handleCloudtentaclesUseSku"
      :handle-cloudtentacles-fetch-knapsack="handleCloudtentaclesFetchKnapsack"
      :handle-cloudtentacles-fetch-vn-list="handleCloudtentaclesFetchVnList"
      :handle-cloudtentacles-appoint-vn="handleCloudtentaclesAppointVn"
      :handle-cloudtentacles-generate-vn-login-code="handleCloudtentaclesGenerateVnLoginCode"
      :handle-cloudtentacles-fetch-vn-code="handleCloudtentaclesFetchVnCode"
      :handle-cloudtentacles-verify-vn-code="handleCloudtentaclesVerifyVnCode"
      :handle-cloudtentacles-fetch-bind-url="handleCloudtentaclesFetchBindUrl"
      :handle-cloudtentacles-back-vn="handleCloudtentaclesBackVn"
      :handle-cloudtentacles-run-full-flow="handleCloudtentaclesRunFullFlow"
    />

    <AdminCloudtentaclesResultCard
      :cloudtentacles-login-result="cloudtentaclesLoginResult"
      :cloudtentacles-validate-result="cloudtentaclesValidateResult"
    />
  </div>
</template>

<style scoped>
.platform-section {
  display: grid;
  gap: var(--space-4);
}
</style>
