package main

import (
	"fmt"
	"log"
	"os/exec"
	"strings"

	pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
	reg "git.zhiweidata.top/taotengfei/AD-Control-Golang/registry"
)

func checkOffice(path string) bool {
	out, err := execcmd(path, "/dstatus")
	if err != nil {
		log.Println(out)
		return false
	}
	outstr := string(out)
	if strings.Contains(outstr, "---LICENSED---") {
		log.Printf("当前已激活Office")
		return false
	} else if strings.Contains(outstr, "No installed product keys detected") {
		log.Printf("当前未激活Office")
		return true
	}
	return false
}
func getOfficeVersion(path string) (string, error) {
	var officeVersion string

	out, err := execcmd_filter(path, "/dstatus", `"LICENSE.NAME"`)
	if err != nil {
		return "", err
	}
	outstr := string(out)
	if strings.Contains(outstr, "Office16ProPlus") {
		log.Printf("Office版本为2016专业增强版")
		officeVersion = "Office 16.0 ProPlus"
	} else {
		return "", fmt.Errorf("查询错误或不支持该版本: %s", outstr)
	}
	return officeVersion, err
}
func activeOffice() error {
	log.Printf("正在检查Office的安装路径")
	path := reg.Query_key_one(reg.HKLM, `SOFTWARE\Microsoft\Office\16.0\Outlook\InstallRoot`, "Path")
	if path == "" {
		return fmt.Errorf("不支持该版本的Office")
	}
	log.Printf("Office的安装路径:%s", path)

	log.Printf("检查激活Office状态")
	if !checkOffice(path) {
		return nil
	}
	log.Printf("准备激活Office")

	// log.Printf("获取Office版本")
	// officeVersion, err := getOfficeVersion(path)
	// if err != nil {
	// 	return err
	// }

	// 从服务器获取密钥
	officeVersion := "Office 16.0 ProPlus"
	pub.SendBoomServer(pub.Msg_Boom_Active, officeVersion)
	if pub.MS.Error {
		return fmt.Errorf(pub.MS.Errmsg)
	}
	key := pub.MS.S
	log.Printf("请求到%s的版本的密钥为%s", officeVersion, key)
	out, err := execcmd(path, "/sethst:192.168.0.21")
	outstr := string(out)
	if err != nil {
		if strings.Contains(outstr, "0x80070005") {
			return fmt.Errorf("请使用管理员权限打开本程序")
		} else if strings.Contains(outstr, "The Software Licensing Service reported that the action requires administrator privilege") {
			return fmt.Errorf("请使用管理员权限打开本程序")
		}
	}
	if strings.Contains(outstr, "Successfully applied setting") {
		log.Printf("kms的IP设置成功")
	}
	out, err = execcmd(path, "/setprt:1688")
	outstr = string(out)
	if err != nil {
		if strings.Contains(outstr, "0x80070005") {
			return fmt.Errorf("请使用管理员权限打开本程序")
		} else if strings.Contains(outstr, "The Software Licensing Service reported that the action requires administrator privilege") {
			return fmt.Errorf("请使用管理员权限打开本程序")
		}
	}
	if strings.Contains(outstr, "Successfully applied setting") {
		log.Printf("kms的port设置成功")
	} else {
		log.Println(outstr)
	}

	out, err = execcmd(path, "/inpkey:"+key)
	outstr = string(out)

	if err != nil {
		if strings.Contains(outstr, "0x80070005") {
			return fmt.Errorf("请使用管理员权限打开本程序")
		} else if strings.Contains(outstr, "The Software Licensing Service reported that the action requires administrator privilege") {
			return fmt.Errorf("请使用管理员权限打开本程序")
		} else if strings.Contains(outstr, "The Software Licensing Service reported that the product key is invalid") {
			return fmt.Errorf("激活使用了错误的密钥")
		}
	}
	if strings.Contains(outstr, "Product key installation successful") {
		log.Printf("产品密钥激活成功")
	} else {
		log.Println(outstr)
	}
	return nil

}

func execcmd(path, args1 string) ([]byte, error) {
	cmd := exec.Command("powershell", `cscript`, "OSPP.VBS", args1, "//nologo")
	cmd.Dir = path
	out, err := cmd.CombinedOutput()
	if err != nil {
		return pub.ToChineseChar(out), err
	}
	return pub.ToChineseChar(out), nil
}

func execcmd_filter(path, args1, args2 string) ([]byte, error) {
	cmd := exec.Command("powershell", `cscript`, "OSPP.VBS", args1, "//nologo", "|", "findstr", args2)
	cmd.Dir = path
	out, err := cmd.CombinedOutput()
	if err != nil {
		return pub.ToChineseChar(out), err
	}
	return pub.ToChineseChar(out), nil
}
