package service

import (
	"encoding/json"
	"fmt"
	"github.com/go-kratos/kratos/v2/transport/http"
	"io"
	http2 "net/http"
	"strconv"
	"voucher/internal/biz/bo"
	"voucher/internal/biz/do"
)

func (this *CmbService) RetryOrderNotice(ctx http.Context) error {

	bodyBytes, err := io.ReadAll(ctx.Request().Body)
	if err != nil {
		return err
	}

	if bodyBytes == nil {
		return fmt.Errorf("bodyBytes is empty")
	}

	var req *do.RetryQueryNotice
	if err = json.Unmarshal(bodyBytes, &req); err != nil {
		return err
	}

	return this.VoucherBiz.PushRetryOrderNotice(ctx, bodyBytes)
}

func (this *CmbService) OrderNotifyRetry(ctx http.Context) error {

	bodyBytes, err := io.ReadAll(ctx.Request().Body)
	if err != nil {
		return err
	}

	var req *do.OrderNotifyRetry
	if err = json.Unmarshal(bodyBytes, &req); err != nil {
		return err
	}

	if req == nil {
		return fmt.Errorf("req is empty")
	}

	if req.StartTime == "" || req.EndTime == "" {
		return fmt.Errorf("start_time or end_time is empty")
	}

	return this.VoucherBiz.PushOrderNotifyRetry(ctx, req)
}

func (this *CmbService) NotifyRetry(ctx http.Context) error {
	id := ctx.Vars().Get("id")
	if id == "" {
		return fmt.Errorf("id is empty")
	}

	orderNotifyId, err := strconv.ParseUint(id, 10, 64)
	if err != nil {
		return err
	}

	return this.VoucherBiz.PushNotifyRetryDelayMQ(ctx, 1, orderNotifyId)
}

var ds = map[string]bool{
	"13474987525": true,
	"15221117226": true,
	"18666173766": true,
}

func (this *CmbService) QueryOrder(ctx http.Context) error {

	phone := ctx.Vars().Get("phone")
	if phone == "" {
		return fmt.Errorf("phone is empty")
	}

	_, ok := ds[phone]
	if !ok {
		return fmt.Errorf("无权限~")
	}

	orderNo := ctx.Vars().Get("order_no")
	if orderNo == "" {
		return fmt.Errorf("orderNo is empty")
	}

	isNotice := ctx.Vars().Get("is_notice")

	str, err := this.VoucherBiz.QueryOrder(ctx, orderNo, isNotice)
	if err != nil {
		return err
	}

	ctx.Response().Header().Add("Content-Type", "text/plain; charset=utf-8")
	ctx.Response().WriteHeader(http2.StatusOK)
	_, err = ctx.Response().Write([]byte(str))
	return err
}

func (this *CmbService) QueryStock(ctx http.Context) error {

	no := ctx.Vars().Get("product_no")
	if no == "" {
		return fmt.Errorf("product_no is empty")
	}

	resp, err := this.VoucherBiz.CmbProductQuery(ctx, no)
	if err != nil {
		return err
	}

	return ctx.JSON(http2.StatusOK, resp)
}

func (this *CmbService) PushWechatQuery(ctx http.Context) error {

	bodyBytes, err := io.ReadAll(ctx.Request().Body)
	if err != nil {
		return err
	}

	var req *do.WechatQuery
	if err = json.Unmarshal(bodyBytes, &req); err != nil {
		return err
	}

	if req == nil {
		return fmt.Errorf("req is empty")
	}

	if req.StartTime == "" || req.EndTime == "" {
		return fmt.Errorf("start_time or end_time is empty")
	}

	if err = this.VoucherBiz.PushWechatQuery(ctx, req); err != nil {
		return err
	}

	return ctx.JSON(http2.StatusOK, map[string]interface{}{
		"data": req,
	})
}

func (this *CmbService) TimeSliceQueryPush(ctx http.Context) error {

	bodyBytes, err := io.ReadAll(ctx.Request().Body)
	if err != nil {
		return err
	}

	var req *do.RdsWechatQuery
	if err = json.Unmarshal(bodyBytes, &req); err != nil {
		return err
	}

	if req == nil {
		return fmt.Errorf("req is empty")
	}

	if req.StartTime == "" || req.EndTime == "" {
		return fmt.Errorf("start_time or end_time is empty")
	}

	if req.GoNum > 10 {
		return fmt.Errorf("协程数量不能大于10")
	}

	_, err = this.timeSliceQuery.Push(ctx, req)
	if err != nil {
		return err
	}

	return ctx.JSON(http2.StatusOK, req)
}

func (this *CmbService) PushWechatRetry(ctx http.Context) error {

	batchNo := ctx.Vars().Get("batch_no")
	if batchNo == "" {
		return fmt.Errorf("batch_no is empty")
	}

	err := this.VoucherBiz.PushWechatRetry(ctx, batchNo)
	if err != nil {
		return err
	}

	return ctx.JSON(http2.StatusOK, map[string]interface{}{
		"data": batchNo,
	})
}

func (this *CmbService) WarningBudget(ctx http.Context) error {

	id := ctx.Vars().Get("id")
	if id == "" {
		return fmt.Errorf("id is empty")
	}

	int64Id, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		return err
	}

	if err = this.VoucherBiz.Warning(ctx, int32(int64Id)); err != nil {
		return err
	}

	return ctx.JSON(http2.StatusOK, map[string]interface{}{
		"data": id,
	})
}

func (this *CmbService) SpecifyNotification(ctx http.Context) error {

	bodyBytes, err := io.ReadAll(ctx.Request().Body)
	if err != nil {
		return err
	}

	var req *bo.FindInBatchesBo
	if err = json.Unmarshal(bodyBytes, &req); err != nil {
		return err
	}

	if req == nil {
		return fmt.Errorf("req is empty")
	}

	if req.OutBizNos == nil && req.OrderNos == nil && req.VoucherNos == nil {
		return fmt.Errorf("out_biz_no or order_no or voucher_no is empty")
	}

	err = this.VoucherBiz.PushRetryNotify(ctx, req)
	if err != nil {
		return err
	}

	return ctx.String(http2.StatusOK, string(bodyBytes))
}

func (this *CmbService) UsedNotifyPush(ctx http.Context) error {

	bodyBytes, err := io.ReadAll(ctx.Request().Body)
	if err != nil {
		return err
	}

	var req *do.WechatUsedQuery
	if err = json.Unmarshal(bodyBytes, &req); err != nil {
		return err
	}

	if req == nil {
		return fmt.Errorf("req is empty")
	}

	if req.StartTime == "" && req.EndTime == "" {
		return fmt.Errorf("start time or end time is empty")
	}

	err = this.VoucherBiz.UsedNotifyPush(ctx, req)
	if err != nil {
		return err
	}

	return ctx.String(http2.StatusOK, string(bodyBytes))
}
