package util

import (
	"fmt"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"io/ioutil"
	"log"
	"os"

	"github.com/fonchain/utils/utils"
	"github.com/golang/freetype"
	"github.com/nfnt/resize"
	"golang.org/x/image/font"
)

func CreateQrCode(invitedCode, userName string) error {
	QrCodePath, err := utils.GenQRCode("https://artist.fontree.cn/login?invitedCode=" + invitedCode)
	if err != nil {
		return err
	}

	tmp, err := os.Open(QrCodePath)
	if err != nil {
		return err

	}
	defer tmp.Close()

	src, err := os.Open("./qrcodebg.png")
	if err != nil {
		log.Println(err)
		return err
	}
	defer src.Close()

	img, err := png.Decode(src)
	if err != nil {
		log.Println(err)
		return err
	}

	outimage, _ := addLabel(img, userName+"邀请您注册画家宝用户", 210, 300, color.RGBA{255, 255, 255, 255}, 55, "font1716.ttf")
	outimage, _ = addLabel(outimage, "(使用此二维码后,"+userName+"将成为你的邀请人)", 210, 400, color.RGBA{255, 255, 255, 255}, 38, "font1716.ttf")
	outimage, _ = addLabel(outimage, "邀请码:"+invitedCode, 260, 1340, color.RGBA{69, 137, 239, 255}, 70, "font1716.ttf")
	QrCode2Path := "static/qrcode/" + invitedCode + "-2.png"
	f, err := os.Create(QrCode2Path)
	if err != nil {
		log.Println(err)
		return err
	}
	defer f.Close()

	newImg := image.NewNRGBA(image.Rect(0, 0, 1125, 2436))
	// fe, err := os.Open("./" + artistPhoto.SmallPic + "_small.jpg")
	qrImg, err := png.Decode(tmp)
	if err != nil {
		fmt.Println(err.Error())
		return err
	}

	qrImg = resize.Resize(uint(700), uint(700), qrImg, resize.Lanczos3)
	draw.Draw(newImg, newImg.Bounds(), outimage, outimage.Bounds().Min.Sub(image.Pt(0, 0)), draw.Over)
	draw.Draw(newImg, newImg.Bounds(), qrImg, qrImg.Bounds().Min.Sub(image.Pt(210, 570)), draw.Over)
	err = png.Encode(f, newImg)
	if err != nil {
		return err
	}
	tmp.Close()

	tmps, err := os.OpenFile(QrCodePath, os.O_RDWR|os.O_CREATE, 0777)
	if err != nil {
		return err
	}
	defer tmps.Close()

	_, err = UploadToBos(tmps, fmt.Sprintf("artistmgmt/static/qrcode/%v.png", invitedCode))
	if err != nil {
		return err
	}
	// fmt.Println(urlss)
	tmp2, err := os.Open(QrCode2Path)
	if err != nil {
		return err
	}
	defer tmp2.Close()

	str, err := UploadToBos(tmp2, fmt.Sprintf("artistmgmt/static/qrcode/%v-2.png", invitedCode))
	if err != nil {
		return err
	}
	fmt.Println(str, "===============")
	return nil
}

func addLabel(img image.Image, label string, x, y int, fontColor color.Color, size float64, fontPath string) (image.Image, error) {
	bound := img.Bounds()
	// 创建一个新的图片
	rgba := image.NewRGBA(image.Rect(0, 0, bound.Dx(), bound.Dy()))
	// 读取字体
	fontBytes, err := ioutil.ReadFile(fontPath)
	if err != nil {
		return rgba, err
	}
	myFont, err := freetype.ParseFont(fontBytes)
	if err != nil {
		return rgba, err
	}

	draw.Draw(rgba, rgba.Bounds(), img, bound.Min, draw.Src)
	c := freetype.NewContext()
	c.SetDPI(72)
	c.SetFont(myFont)
	c.SetFontSize(size)
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)
	uni := image.NewUniform(fontColor)
	c.SetSrc(uni)
	c.SetHinting(font.HintingNone)

	// 在指定的位置显示
	pt := freetype.Pt(x, y+int(c.PointToFixed(size)>>6))
	if _, err := c.DrawString(label, pt); err != nil {
		return rgba, err
	}

	return rgba, nil
}
