package cmd

import (
	"agent-wdd/host_info"
	"agent-wdd/log"
	"agent-wdd/op"
	"agent-wdd/utils"

	"github.com/spf13/cobra"
)

func addZshSubcommands(cmd *cobra.Command) {
	cmd.Run = func(cmd *cobra.Command, args []string) {
		log.Info("安装zsh")

		// 初始化安装命令
		utils.CreateFolder("/root/wdd")

		if host_info.CanConnectInternet() <= 1 {
			log.Error("无法连接互联网，无法安装zsh")
			return
		}

		// 判定config
		if host_info.ConfigCache.Agent.Network.Public.IPv4 == "" {
			network := host_info.ConfigCache.Agent.Network
			network.Gather()
			network.SaveConfig()
		}

		// 下载并安装zsh和git
		ok := op.AgentPackOperator.Install([]string{"git", "zsh"})
		if !ok {
			log.Error("安装git和zsh失败, 无法继续后面的的安装！")
			return
		}

		// 清理残留
		utils.RemoveFolderComplete("/root/.oh-my-zsh")
		utils.RemoveFolderComplete("/root/wdd/zsh-install.sh")

		ohMyZshInstallUrl := ""

		if host_info.ConfigCache.Agent.Network.Internet == 7 {
			ohMyZshInstallUrl = "https://gitea.107421.xyz/zeaslity/ohmyzsh/raw/branch/master/tools/install.sh"
		} else {
			ohMyZshInstallUrl = "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh"
		}

		downloadOk, resultLog := utils.DownloadFile(ohMyZshInstallUrl, "/root/wdd/zsh-install.sh")
		if !downloadOk {
			log.Error("下载zsh安装脚本失败: %v", resultLog)
			return
		}

		op.SingleLineCommandExecutor([]string{
			"chmod",
			"+x",
			"/root/wdd/zsh-install.sh",
		})

		installZshCommand := []string{
			"sh",
			"-c",
			"/root/wdd/zsh-install.sh",
		}

		if host_info.ConfigCache.Agent.Network.Internet == 7 {
			op.SingleLineCommandExecutor([]string{
				"export",
				"REMOTE=https://gitea.107421.xyz/zeaslity/ohmyzsh.git",
			})
		}

		// 执行 oh-my-zsh 安装命令
		op.RealTimeCommandExecutor(installZshCommand)

		log.Info("安装zsh完成, 开始安装插件!")

		pluginsHardCodeUrl := [][]string{
			{
				"https://gitee.com/wangl-cc/zsh-autosuggestions.git",
				"/root/.oh-my-zsh/plugins/zsh-autosuggestions",
			},
			{
				"https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git",
				"/root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
			},
		}

		pluginsHardCodeOutsideUrl := [][]string{
			{
				"https://github.com/zsh-users/zsh-autosuggestions",
				"/root/.oh-my-zsh/plugins/zsh-autosuggestions",
			},
			{
				"https://github.com/zsh-users/zsh-syntax-highlighting.git",
				"/root/.oh-my-zsh/plugins/zsh-syntax-highlighting",
			},
		}

		pluginsListUrl := []string{
			"https://gitea.107421.xyz/zeaslity/ohmyzsh/src/branch/master/plugins/command-not-found/command-not-found.plugin.zsh",
			"https://gitea.107421.xyz/zeaslity/ohmyzsh/src/branch/master/plugins/autojump/autojump.plugin.zsh",
			"https://gitea.107421.xyz/zeaslity/ohmyzsh/src/branch/master/plugins/themes/themes.plugin.zsh",
		}

		// 下载插件
		if host_info.ConfigCache.Agent.Network.Internet == 7 {
			// 执行硬编码命令, 安装插件
			for _, command := range pluginsHardCodeUrl {
				op.SingleLineCommandExecutor([]string{"git", "clone", command[0], command[1]})
			}

		} else {
			// 执行硬编码命令, 安装插件 国外
			for _, command := range pluginsHardCodeOutsideUrl {
				op.SingleLineCommandExecutor([]string{"git", "clone", command[0], command[1]})
			}
		}
		// 下载插件
		for _, url := range pluginsListUrl {
			downloadOk, resultLog := utils.DownloadFile(url, "/root/.oh-my-zsh/plugins/")
			if !downloadOk {
				log.Error("下载插件失败: %v", resultLog)
			}
		}

		// 修改ZSH配置
		utils.FindAndReplaceContentInFile("robbyrussell", "agnoster", "/root/.zshrc")
		utils.FindAndReplaceContentInFile("# DISABLE_AUTO_UPDATE=\"true\"", "DISABLE_AUTO_UPDATE=\"true\"", "/root/.zshrc")
		utils.FindAndReplaceContentInFile("plugins=(git)", "plugins=(git zsh-autosuggestions zsh-syntax-highlighting command-not-found z themes)", "/root/.zshrc")

		//
		op.SingleLineCommandExecutor([]string{
			"source",
			"/root/.zshrc",
		})

		op.SingleLineCommandExecutor([]string{
			"chsh",
			"-s",
			"/bin/zsh",
		})

		op.SingleLineCommandExecutor([]string{
			"zsh",
		})

		log.Info("Zsh安装完成")
	}
}
