package getweb

import (
	"git.lxtend.com/lixiangwuxian/qqbot/model"
	"git.lxtend.com/lixiangwuxian/qqbot/qq_message"
	"git.lxtend.com/lixiangwuxian/qqbot/util"
)

func init() {
	// handler.RegisterHandler("上网", getweb, constants.LEVEL_TRUSTED)
	// handler.RegisterHelpInform("上网", "getweb", "上网 <URL> 截取网页")
}

func getweb(msg model.Message) (reply *model.Reply) {
	if len(msg.RawMsg) <= len("上网 ") {
		return &model.Reply{}
	}
	url := msg.RawMsg[len("上网 "):]
	url = formatURL(url)
	// if err := util.ScreenshotURL(url, "./tmp/getweb/url.png", 1620, 1960, 0, 0, 0, 0, ""); err != nil {
	if err := util.ScreenshotURL(url, util.GenTempFilePath("getweb/url.png"), 1620, 1960, 0, 0, 0, 0, ""); err != nil {
		return &model.Reply{
			ReplyMsg:       err.Error(),
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	imageMsg := qq_message.ImageMessage{
		Type: "image",
		Data: qq_message.ImageMessageData{
			File: "file:///tmp/qqbot/getweb/url.png",
		},
	}
	return &model.Reply{
		ReplyMsg:       imageMsg.ToCQString(),
		ReferOriginMsg: true,
		FromMsg:        msg,
	}
}

func formatURL(url string) string {
	if url[:7] != "http://" && url[:8] != "https://" {
		return "http://" + url
	}
	return url
}
