package api

import (
	"fmt"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	"github.com/goWxHook/goWxHook/core"
	"github.com/goWxHook/goWxHook/docs"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"log/slog"
)

type WebApi struct {
	WxApi *core.WxApi
	Port  int
}

func NewWebApi(wxApi *core.WxApi, port int) *WebApi {
	return &WebApi{
		WxApi: wxApi,
		Port:  port,
	}
}

func (w *WebApi) StartWebApi() error {
	gin.SetMode(gin.ReleaseMode)
	app := gin.New()
	app.Use(gin.Recovery())
	api := app.Group("/api")
	v1 := api.Group("/v1")

	// swagger doc
	docs.SwaggerInfo.Host = fmt.Sprintf("127.0.0.1:%d", w.Port)
	docs.SwaggerInfo.Schemes = []string{"http"}

	url := ginSwagger.URL("/api/swagger/doc.json")
	app.GET("/api/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

	pprof.Register(app, "/api/v1/debug/pprof")

	// 登录接口
	v1.GET("/login/version", w.getWxVersion)
	v1.GET("/login/userinfo", w.getLoginUserInfo)
	v1.POST("/login/qrcode/refresh", w.refreshLoginQrCode)
	v1.POST("/login/logout", w.wxLogout)
	v1.POST("/login/exit", w.wxExit)

	// 联系人接口
	v1.GET("/contact/friend/list", w.getFriendList)
	v1.GET("/contact/friend/:wxid", w.getFriendInfo)
	v1.GET("/contact/friend/protocol/brief/:wxid", w.getFriendBriefInfo)
	v1.GET("/contact/friend/protocol/detail/:wxid", w.getFriendDetailInfo)
	v1.POST("/contact/friend/protocol/detail", w.batchGetFriendDetailInfo)
	v1.POST("/contact/friend/remark", w.editFriendRemark)
	v1.POST("/contact/friend/add", w.addFriend)
	v1.POST("/contact/friend/accept", w.acceptFriend)
	v1.POST("/contact/friend/delete/:wxid", w.deleteFriend)
	v1.GET("/contact/friend/search", w.searchWxUser)
	v1.POST("/contact/friend/add/search", w.addSearchWxUser)
	v1.POST("/contact/friend/check/:wxid", w.checkFriend)
	v1.POST("/contact/friend/add/auto", w.autoAcceptAddFriend)
	v1.POST("/contact/friend/card/auto", w.autoAcceptCard)
	v1.POST("/contact/accept/wc_pay/auto", w.autoAcceptWCPay)
	v1.POST("/contact/public/list", w.getPublicUserList)

	v1.GET("/contact/room/list", w.getRoomList)
	v1.GET("/contact/room/member/list/:roomWxid", w.getRoomMemberList)
	v1.GET("/contact/room/protocol/detail/:roomWxid", w.getRoomDetailInfo)
	v1.GET("/contact/room/protocol/invite/list/:roomWxid", w.getRoomInviteRelationList)
	v1.POST("/contact/room/create", w.createRoom)
	v1.POST("/contact/room/protocol/create", w.createRoomByProtocol)
	v1.POST("/contact/room/member/invite", w.inviteToRoom)
	v1.POST("/contact/room/member/delete", w.deleteRoomMember)
	v1.POST("/contact/room/name/modify", w.editRoomName)
	v1.POST("/contact/room/notice/modify", w.editRoomNotice)
	v1.POST("/contact/room/mine/nickname/modify", w.editRoomMineNickName)
	v1.POST("/contact/room/member/nickname/show", w.showRoomNickName)
	v1.POST("/contact/room/to_contact/save", w.roomSaveToContact)
	v1.POST("/contact/room/friend/add", w.addRoomMemberToFriend)
	v1.POST("/contact/room/delete/:roomWxid", w.deleteRoom)
	v1.POST("/contact/room/accept/auto", w.autoAcceptRoom)

	v1.POST("/message/send/text", w.sendTextMsg)
	v1.POST("/message/send/card", w.sendCardMsg)
	v1.POST("/message/send/link", w.sendLinkMsg)
	v1.POST("/message/send/image", w.sendImageMsg)
	v1.POST("/message/send/file", w.sendFileMsg)
	v1.POST("/message/send/video", w.sendVideoMsg)
	v1.POST("/message/send/gif", w.sendGifMsg)
	v1.POST("/message/send/xml", w.sendXmlMsg)
	v1.POST("/message/send/forward", w.sendForwardMsg)
	v1.POST("/message/register_msg_http_callback", w.registerMsgHttpCallBack)

	v1.POST("/cdn/initial", w.cdnInitial)
	v1.POST("/cdn/upload", w.cdnUpload)
	v1.POST("/cdn/download", w.cdnDownload)
	v1.POST("/cdn/workwx/download", w.cdnWorkWxDownload)
	v1.POST("/cdn/send/text", w.cdnSendTextMsg)
	v1.POST("/cdn/send/image", w.cdnSendImageMsg)
	v1.POST("/cdn/send/video", w.cdnSendVideoMsg)
	v1.POST("/cdn/send/file", w.cdnSendFileMsg)
	v1.POST("/cdn/send/card_link", w.cdnSendCardLinkMsg)
	v1.POST("/cdn/send/gif", w.cdnSendGifMsg)
	v1.POST("/cdn/send/mini_program", w.cdnSendMiniProgramMsg)
	v1.POST("/cdn/send/video_number", w.cdnSendVideoMomentMsg)
	v1.POST("/cdn/send/card", w.cdnSendCardMsg)
	v1.POST("/cdn/send/location", w.cdnSendLocationMsg)
	v1.POST("/cdn/send/revoke", w.cdnSendRevokeMsg)
	v1.POST("/cdn/send/xml", w.cdnSendXmkMsg)
	v1.POST("/cdn/send/gif_new", w.cdnSendGifMsgNew)

	v1.GET("/workwx/user/list", w.getWorkWxUserList)
	v1.GET("/workwx/room/list", w.getWorkWxRoomList)
	v1.POST("/workwx/room/member/list", w.getWorkWxRoomMemberList)

	v1.GET("/collect/list", w.getCollectList)
	v1.POST("/collect/send", w.sendCollect)
	v1.POST("/collect/send/msgid", w.sendCollectMsgByMsgId)

	v1.GET("/tag/list", w.getTagList)
	v1.POST("/tag/add", w.addTag)
	v1.POST("/tag/delete", w.deleteTag)
	v1.POST("/tag/modify", w.modifyTag)
	v1.POST("/tag/add/user", w.addTagToUser)
	v1.POST("/tag/list/user", w.getTagListByWxid)

	v1.POST("/voice/to/text", w.voiceToText)

	v1.POST("/mini/program/code", w.getMiniProgramCode)

	v1.POST("/ui/switch/session", w.switchSession)
	v1.POST("/ui/clear/chat/record", w.clearChatRecord)
	v1.POST("/ui/chat/msg/not/notify", w.chatMsgNotNotify)
	v1.POST("/ui/chat/session/top", w.chatSessionTop)

	v1.POST("/moment/list", w.getMoment)
	v1.POST("/moment/comment", w.commentMoment)
	v1.POST("/moment/like", w.likeMoment)
	v1.POST("/moment/send", w.sendMoment)
	v1.POST("/moment/upload/image", w.uploadMomentImage)
	v1.POST("/moment/friend/list", w.getFriendMoment)

	v1.POST("/video_moment/init", w.videoMomentInit)
	v1.POST("/video_moment/search", w.videoMomentSearch)
	v1.POST("/video_moment/user/home", w.videoMomentUserHome)
	v1.POST("/video_moment/video/detail", w.videoMomentVideoDetail)
	v1.POST("/video_moment/follow", w.videoMomentFollow)
	v1.POST("/video_moment/like", w.videoMomentLike)
	v1.POST("/video_moment/get/session_id", w.videoMomentGetSessionId)
	v1.POST("/video_moment/send/msg", w.videoMomentSendMsg)
	v1.POST("/video_moment/create/virtual_nickname", w.videoMomentCreateVirtualNickName)
	v1.POST("/video_moment/switch/virtual_nickname", w.videoMomentSwitchVirtualNickName)
	v1.POST("/video_moment/delete/virtual_nickname", w.videoMomentDeleteVirtualNickName)

	v1.POST("/live/enter", w.liveEnter)
	v1.POST("/live/get/online/user", w.liveGetOnlineUser)
	v1.POST("/live/get/change/info", w.liveGetChangeInfo)
	v1.POST("/live/send/msg", w.liveSendMsg)
	v1.POST("/live/get/shelf", w.liveGetShelf)

	v1.POST("/other/get_a8_key", w.getA8Key)
	v1.POST("/other/decrypt_img", w.decryptImg)

	// 启动一个API服务
	slog.Info("启动服务WEB服务", "API监听", fmt.Sprintf("http://127.0.0.1:%d", w.Port),
		"接口文档", fmt.Sprintf("http://127.0.0.1:%d%s", w.Port, "/api/swagger/index.html"))
	return app.Run(fmt.Sprintf(":%d", w.Port))
}
