<script lang="ts">
	import { Button } from '$lib/components/ui/button/index.js';
	import { Label } from '$lib/components/ui/label/index.js';
	import { Input } from '$lib/components/ui/input/index.js';
	import * as Card from '$lib/components/ui/card/index.js';

	import StatusHealth from '$lib/assets/status-health.svelte';
	import * as adb from '$lib/core/adb/adb';
	import Checkbox from '$lib/components/ui/checkbox/checkbox.svelte';
	import { TerminalComponent } from '$lib/components/ui/terminal';
	import { onMount } from 'svelte';
	import { goto } from '$app/navigation';
	import { auth } from '$lib/core/client/firebase';
	import { auth as authStore } from '$lib/core/stores/auth';

	// const device = await usb.requestDevice({
	// 	filters: [
	// 		{
	// 			vendorId: 0x18d1
	// 		}
	// 	]
	// });

	let recipe: any | undefined = undefined;

	async function test_adb() {
		try {
			if (!('usb' in navigator)) {
				throw new Error('WebUSB not supported, try using fallback');
			}

			await adb.connnectViaWebUSB();

			let instance = adb.getAdbInstance();

			if (instance) {
				console.log('create instance ok');
				let result = await adb.executeCmd(
					'am start -n com.forthvending.coffeemain/com.forthvending.coffeemain.MainActivity'
				);
				console.log(result);
			}
		} catch (e) {
			console.error(e);
		}
	}

	async function test_send_command() {
		try {
			if (!('usb' in navigator)) {
				throw new Error('WebUSB not supported, try using fallback');
			}

			let instance = adb.getAdbInstance();

			if (instance) {
				let txt = document.getElementById('cmd-input') as HTMLInputElement;

				console.log('instance existed, ', txt.value);
				let result = await adb.executeCmd(txt.value ?? '');
				console.log(result);
			}
		} catch (e) {
			console.error(e);
		}
	}

	async function test_pull_recipe_dev() {
		try {
			if (!('usb' in navigator)) {
				throw new Error('WebUSB not supported, try using fallback');
			}
			let instance = adb.getAdbInstance();
			if (instance) {
				let result = await adb.pull('/sdcard/coffeevending/cfg/recipe_branch_dev.json');
				let payload = JSON.parse(result ?? '');
				console.log(payload);
				recipe = payload;
				alert('pull completed!');
			}
		} catch (e) {
			console.error(`[PULL] ${e}`);
		}
	}
</script>

<!--
<button
	class="bg-white px-4 py-2 border flex gap-2 border-slate-200 rounded-lg text-slate-700 hover:border-slate-400 hover:text-slate-900 hover:shadow transition duration-150"
	onclick={logout}
>
	<span>Logout</span>
</button> -->
