package restart

import (
	"errors"
	"log"
	"os"
	"os/exec"
	"time"

	"git.lxtend.com/qqbot/constants"
	"git.lxtend.com/qqbot/handler"
	"git.lxtend.com/qqbot/model"
	docker "git.lxtend.com/qqbot/service/exec"
)

var hasVaildBuild = true

func init() {
	handler.RegisterHandler("/重启bot", restart, constants.LEVEL_ADMIN)
	handler.RegisterHelpInform("/重启bot", "热更新", "重启bot")
	handler.RegisterHandler("/构建bot", build, constants.LEVEL_ADMIN)
	handler.RegisterHelpInform("/构建bot", "热更新", "构建bot")
	handler.RegisterHandler("/构建重启", buildAndRestart, constants.LEVEL_ADMIN)
	handler.RegisterHelpInform("/构建重启", "热更新", "构建并重启")
	handler.RegisterHandler("/gitpull", pullCode, constants.LEVEL_USER)
	handler.RegisterHelpInform("/gitpull", "热更新", "拉取最新代码")
	handler.RegisterHandler("/shutdown", shutdown, constants.LEVEL_ADMIN)
	handler.RegisterHelpInform("/shutdown", "热更新", "结束程序")
}

func restart(msg model.Message) model.Reply {
	if !hasVaildBuild {
		return model.Reply{
			ReplyMsg:       "上次构建失败，请先成功构建再部署",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	go restartProgram()
	return model.Reply{
		ReplyMsg:       "重启中...",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func shutdown(msg model.Message) model.Reply {
	go shutdownProgram()
	return model.Reply{
		ReplyMsg:       "关闭中...",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func build(msg model.Message) model.Reply {
	err := buildBot()
	if err != nil {
		return model.Reply{
			ReplyMsg:       "构建失败,报错如下\n" + err.Error(),
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	return model.Reply{
		ReplyMsg:       "构建成功",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func buildAndRestart(msg model.Message) model.Reply {
	err := buildBot()
	if err != nil {
		return model.Reply{
			ReplyMsg:       "构建失败,报错如下\n" + err.Error(),
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	if !hasVaildBuild {
		return model.Reply{
			ReplyMsg:       "构建失败，请先成功构建再部署",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	go restartProgram()
	return model.Reply{
		ReplyMsg:       "构建成功,重启中...",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func pullCode(msg model.Message) model.Reply {
	err := gitPull()
	if err != nil {
		return model.Reply{
			ReplyMsg:       "拉取代码失败,报错如下\n" + err.Error(),
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	return model.Reply{
		ReplyMsg:       "拉取代码成功",
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func restartProgram() error {
	log.Println("重启程序...")
	docker.DockerContainer.RemoveContainer()
	time.Sleep(500 * time.Millisecond)
	path, err := os.Executable()
	if err != nil {
		return err
	}
	workDir, err := os.Getwd()
	if err != nil {
		return err
	}
	cmd := exec.Command(path)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Dir = workDir
	if err := cmd.Start(); err != nil {
		return err
	}
	os.Exit(0)
	return nil
}

func shutdownProgram() error {
	log.Println("关闭程序...")
	docker.DockerContainer.RemoveContainer()
	time.Sleep(500 * time.Millisecond)
	os.Exit(0)
	return nil
}

func buildBot() error {
	hasVaildBuild = false
	workDir, err := os.Getwd()
	if err != nil {
		return err
	}
	cmd := exec.Command("go", "mod", "tidy")
	cmd.Dir = workDir
	output, err := cmd.CombinedOutput()
	if err != nil {
		return errors.New(string(output) + err.Error())
	}

	cmd = exec.Command("go", "build", "-o", "qqbot")
	cmd.Dir = workDir

	output, err = cmd.CombinedOutput()
	if err != nil {
		return errors.New(string(output) + err.Error())
	}
	hasVaildBuild = true
	return nil
}

func gitPull() error {
	workDir, err := os.Getwd()
	if err != nil {
		return err
	}
	cmd := exec.Command("git", "pull")
	cmd.Dir = workDir

	output, err := cmd.CombinedOutput()
	if err != nil {
		return errors.New(string(output) + err.Error())
	}
	return nil
}
