package GoUtil

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"runtime"
	"server/conf"
	"time"
)

const (
	FEISHU_WEBHOOK = "https://gadmin.bywaystudios.com/api/alibaba/game/notify"
	FEISHU_ORDER   = "https://gadmin.bywaystudios.com/api/alibaba/notify/order"
)

// AAqFpbuPhFSEx
func SendFeishuFatal(PlayerId int, FuncName string, msg string) error {
	if conf.Server.GameName == "pet_home_local" {
		return nil
	}
	// 创建请求体
	stack := make([]byte, 1024)
	length := runtime.Stack(stack, false)
	payload := map[string]interface{}{
		"notify_msg": fmt.Sprintf("游戏接口出错 %s:%d[炸弹][炸弹][炸弹]", conf.Server.GameName, PlayerId),
		"func_name":  FuncName,
		"stack":      fmt.Sprintf("%s\nStack trace:\n%s", msg, stack[:length]),
		"alarm_time": time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05"),
	}

	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		return err
	}

	// 创建HTTP请求
	req, err := http.NewRequest("POST", FEISHU_WEBHOOK, bytes.NewBuffer(payloadBytes))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// 检查响应状态码
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to send message, status code: %d", resp.StatusCode)
	}

	return nil
}

func SendFeishuOrder(PlayerId int, OrderId string, Price float64, ProductName string, PayTime, VerityTime int64, Charge float64) error {
	// 创建请求体

	payload := map[string]interface{}{
		"UID":           PlayerId,
		"OrderId":       OrderId,
		"Product":       String(Price),
		"ProductName":   ProductName,
		"EventRecovery": time.Unix(PayTime, 0).Format("2006-01-02 15:04:05"),
		"EventAge":      time.Unix(VerityTime, 0).Format("2006-01-02 15:04:05"),
		"TotalCharge":   String(Charge),
	}

	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		return err
	}

	// 创建HTTP请求
	req, err := http.NewRequest("POST", FEISHU_ORDER, bytes.NewBuffer(payloadBytes))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// 检查响应状态码
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to send message, status code: %d", resp.StatusCode)
	}

	return nil
}

func SendFeishuMsg2(msg string) error {
	// 创建请求体
	payload := map[string]interface{}{
		"msg_type": "interactive",
		"card": map[string]interface{}{
			"type": "template",
			"data": map[string]interface{}{
				"template_id":           "AAqFpLD2XfySR",
				"template_version_name": "1.0.1",
				"template_variable": map[string]interface{}{
					"appName": conf.Server.GameName,
				},
			},
		},
	}

	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		return err
	}

	// 创建HTTP请求
	req, err := http.NewRequest("POST", FEISHU_WEBHOOK, bytes.NewBuffer(payloadBytes))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// 检查响应状态码
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to send message, status code: %d", resp.StatusCode)
	}

	return nil
}
