package site

import (
	"fmt"
	"github.com/dustin/go-humanize"
	datastar "github.com/starfederation/datastar/sdk/go"
	"net/http"
	"path/filepath"
	"strings"
)

templ PageBundler(r *http.Request, manifest PluginManifest, signals *BundlerSignals) {
	@Page("Bundler", "Bundle only the plugins you need to reduce the size of Datastar even further.", "/bundler") {
		@header(r)
		<div data-signals={ templ.JSONString(signals) } class="p-8 flex flex-col gap-4">
			<div>
				<div class="text-6xl font-brand font-bold text-center">Bundler</div>
				<div class="text-center">While Datastar is still one of the smallest frameworks available, you can bundle only the plugins you need to reduce the size even further.</div>
			</div>
			<div id="results"></div>
			<button class="btn btn-primary btn-block" data-on-click={ datastar.PostSSE("/bundler") }>
				@icon("material-symbols:build")
				Bundle
			</button>
			<div class="flex flex-col gap-2">
				<div class="flex justify-between items-center">
					<div class="font-bold text-3xl">Plugins</div>
					<div class="flex gap-2">
						<button class="btn" data-on-click="toggleAll('static_library_source_plugins_')">Toggle</button>
						<button class="btn" data-on-click="setAll('static_library_source_plugins_',true)">All</button>
						<button class="btn" data-on-click="setAll('static_library_source_plugins_',false)">None</button>
					</div>
				</div>
				<div class="flex flex-wrap gap-2 justify-around">
					{{
						currentPath := ""
					}}
					for _, plugin := range manifest.Plugins {
						{{
						signal := fmt.Sprintf("includedPlugins.%s", plugin.Key)
						pluginDir := filepath.Dir(plugin.Path[11:])
						}}
						if currentPath != pluginDir {
							<div class="divider w-full font-mono text-neutral">{ pluginDir }</div>
							{{ currentPath = pluginDir }}
						}
						<details class="collapse collapse-arrow bg-base-200">
							<summary class="collapse-title">
								<div class="flex flex-wrap items-center gap-4 justify-between">
									<div class="flex gap-4 items-center text-xl">
										@icon(plugin.Icon)
										<input class="toggle toggle-success" type="checkbox" data-bind={ signal }/>
										<div class="flex item-center font-mono">
											<div>{ plugin.Label }</div>
										</div>
									</div>
									<div class="text-xs italic">{ plugin.Slug }</div>
								</div>
							</summary>
							<div class="collapse-content">
								<table class="table">
									<tr>
										<td>Name</td>
										<th>{ plugin.Name }</th>
									</tr>
									if plugin.Description !="" {
										<tr>
											<td>Description</td>
											<th>{ plugin.Description }</th>
										</tr>
									}
									<tr>
										<td>Authors</td>
										<th>{ plugin.Authors }</th>
									</tr>
									<tr>
										<td>Path</td>
										<th>{ plugin.Path[11:] }.ts</th>
									</tr>
									<tr>
										<td>Source</td>
										<th>
											<div class="mockup-code">
												{{ lines := strings.Split(plugin.Contents, "\n") }}
												for _, line := range lines {
													if !strings.HasPrefix(line, "//") {
														<pre>{ line }</pre>
													}
												}
											</div>
										</th>
									</tr>
								</table>
							</div>
						</details>
					}
				</div>
			</div>
		</div>
	}
}

templ bundlerResultsFragment(results BundleResults) {
	<div id="results" class="flex gap-8">
		<table class="table w-full">
			<thead>
				<tr>
					<th>Hash</th>
					<th>Compile Time</th>
					<th>Source Size Gzipped</th>
					<th class="text-right">Download</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>{ results.Hash }</td>
					<td>{ results.CompileTime.String() }</td>
					<td>{ humanize.Comma(int64( results.SourceSizeGzipped)) } ({ humanize.IBytes(results.SourceSizeGzipped) }) </td>
					<td class="text-right">
						<a class="btn btn-success" href={ templ.SafeURL(results.DownloadURL) }>
							@icon("material-symbols:folder-zip")
							{ results.Name }.zip
						</a>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
}
