package license

import (
	"fmt"
	"math/rand/v2"
	"testing"
	"time"
)

// 标准输出
func TestLicenseNewStandard(t *testing.T) {
	date := time.Now().AddDate(0, 0, 180)
	curY, curM, curD := date.Date()
	expiration := time.Date(curY, curM, curD, 23, 59, 59, 0, time.Local)
	encryptedKey, err := New(expiration)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Println("许可密钥:", encryptedKey)
	info, err := Stat(encryptedKey)
	if err != nil {
		t.Error(err)
		return
	}
	rand.Int()
	fmt.Println("创建时间:", info.CreateAt().Format(time.DateTime))
	fmt.Println("过期时间:", info.ExpireAt().Format(time.DateTime))
}

// 标准输出, 自定义日期
func TestLicenseNewWithCustom(t *testing.T) {
	expiration := time.Date(2024, 12, 14, 23, 59, 59, 0, time.Local)
	encryptedKey, err := New(expiration)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Println("许可密钥:", encryptedKey)
	info, err := Stat(encryptedKey)
	if err != nil {
		t.Error(err)
		return
	}
	rand.Int()
	fmt.Println("创建时间:", info.CreateAt().Format(time.DateTime))
	fmt.Println("过期时间:", info.ExpireAt().Format(time.DateTime))
}

// 永久授权输出
func TestPerpetualLicenseNew(t *testing.T) {
	expiration := time.Date(9999, 12, 31, 23, 59, 59, 0, time.Local)
	encryptedKey, err := New(expiration)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Println("许可密钥:", encryptedKey)
	info, err := Stat(encryptedKey)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Println("创建时间:", info.CreateAt().Format(time.DateTime))
	fmt.Println("过期时间:", info.ExpireAt().Format(time.DateTime))
}

func TestLicense_New(t *testing.T) {
	d, err := time.ParseDuration("+1m")
	if err != nil {
		t.Fatal(err)
		return
	}
	expiration := time.Now().Add(d)
	// expiration := time.Now().AddDate(0, 0, 1)
	encryptedKey, err := New(expiration)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("密钥长度:", len(encryptedKey))
	t.Log("许可密钥:", encryptedKey)
	info, err := Stat(encryptedKey)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("创建时间:", info.CreateAt().Format(time.DateTime))
	t.Log("过期时间:", info.ExpireAt().Format(time.DateTime))
	// 检查有效期
	if info.Expired() {
		t.Logf("License has expired on: %s", info.ExpireAt())
	} else {
		t.Logf("License expire in %s", info.ExpireAt())
	}
}
