package hook

import (
	"fmt"
	"time"
)

// 退出登陆
func (h *Hook) Logout() (result LogoutResp, err error) {
	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/Logout")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("Logout failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 结束微信
func (h *Hook) TerminateThisWeChat(pid string) (result LogoutResp, err error) {
	resp, err := h.Client.R().SetBody(&TerminateThisWeChatReq{
		PID: pid,
	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.AdminPort + "/TerminateThisWeChat")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("TerminateThisWeChatReq failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 获取微信总数
func (h *Hook) GetWeChatProcessNumber() (result GetWeChatProcessNumberResp, err error) {
	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.AdminPort + "/Get_WeChatProcessNumber")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("GetWeChatProcessNumber failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 点击登陆微信按钮
func (h *Hook) ClickLoginButton() (result ClickLoginButtonResp, err error) {
	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/ClickLoginButton")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("ClickLoginButton failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 启动微信
func (h *Hook) StartWechat(port string) (result StartWechatResp, err error) {
	resp, err := h.Client.R().SetBody(&StartWechatReq{
		StartPort: port,
	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.AdminPort + "/StartWechat")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("StartWechat failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 获取进程端口占用信息
func (h *Hook) GetPortOccupiedInfo(port string) (result GetPortOccupiedInfoResp, err error) {
	resp, err := h.Client.R().SetBody(&GetPortOccupiedInfoReq{
		CheckPort: port,
	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.AdminPort + "/Get_PortOccupiedInfo")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("GetPortOccupiedInfo failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 刷新登陆二维码
func (h *Hook) RefreshLoginQRCode() (err error) {
	resp, err := h.Client.R().Post("http://" + h.ServerIp + ":" + h.WxPort + "/RefreshLoginQRCode")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("RefreshLoginQRCode failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 获取登陆二维码
func (h *Hook) GetLoginQRCode() (result GetLoginQRCodeReap, err error) {
	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/GetLoginQRCode")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("GetLoginQRCode failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 查询登录状态
func (h *Hook) IsLoginStatus() (result IsLoginStatusResp, err error) {
	h.Client.SetTimeout(5 * time.Second)
	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/IsLoginStatus")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("IsLoginStatus failed with status code %d", resp.StatusCode)
		return
	}

	return
}
