package pkg

import (
	"bytes"
	"database/sql"
	"fmt"
	"github.com/gen2brain/beeep"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"
	"xorm.io/xorm"

	_ "github.com/mutecomm/go-sqlcipher/v4"
)

func TestName(t *testing.T) {
	cmd := exec.Command("chcp")
	var b bytes.Buffer
	cmd.Stdout = &b
	cmd.Stderr = &b
	_ = cmd.Run()
	code := strings.Split(string(b.Bytes()), ":")[1]
	fmt.Printf("系统字符集编码为 : %v\n", code)
}

func TestBeeepBeep(t *testing.T) {
	for i := 0; i < 10; i++ {
		go func(freq float64) {
			err := beeep.Beep(freq, beeep.DefaultDuration)
			if err != nil {
				panic(err)
			}
		}(beeep.DefaultFreq + (float64(i * 100)))
	}
}

func TestBeeepNotify(t *testing.T) {
	err := beeep.Notify("测试", "测试", "")
	if err != nil {
		panic(err)
	}

	time.Sleep(1 * time.Second)
}

func TestEnv(t *testing.T) {
	for _, e := range os.Environ() {
		parts := strings.SplitN(e, "=", 2)

		if len(parts) != 2 {
			continue
		} else {
			println(parts[0], parts[1])
		}
	}
}

func TestJavaEnv(t *testing.T) {
	fmt.Println(os.Getenv("java_home"))
	fmt.Println(os.Getenv("JAVA_HOME"))
}

func TestSqlite(t *testing.T) {
	fmt.Println(os.Args[0])
	fmt.Println(os.Getwd())

	dbPath := filepath.Join(filepath.Dir(os.Args[0]), "test.db")
	fmt.Printf("dbPath => %v\n", dbPath)

	// 加密
	//dataSourceName := fmt.Sprintf("file:%s?_pragma_key=%s", dbPath, "shikong")
	// 不加密
	dataSourceName := fmt.Sprintf("file:%s?", dbPath)
	db, err := sql.Open("sqlite3", dataSourceName)
	_, _ = db.Exec("PRAGMA cipher_use_hmac = OFF;")
	_, _ = db.Exec("PRAGMA cipher_page_size = 1024;")
	_, _ = db.Exec("PRAGMA kdf_iter = 4000;")

	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		_ = db.Close()
	}()

	err = db.Ping()
	if err != nil {
		t.Fatal(err)
	}

	//_, err = db.Exec("PRAGMA key = shikong;")
	//if err != nil {
	//	t.Fatal(err)
	//	return
	//}

	sql := `
DROP TABLE IF EXISTS cars;
CREATE TABLE cars(id INTEGER PRIMARY KEY, name TEXT, price INT);
INSERT INTO cars(name, price) VALUES('Audi',52642);
INSERT INTO cars(name, price) VALUES('Mercedes',57127);
INSERT INTO cars(name, price) VALUES('Skoda',9000);
INSERT INTO cars(name, price) VALUES('Volvo',29000);
INSERT INTO cars(name, price) VALUES('Bentley',350000);
INSERT INTO cars(name, price) VALUES('Citroen',21000);
INSERT INTO cars(name, price) VALUES('Hummer',41400);
INSERT INTO cars(name, price) VALUES('Volkswagen',21600);
`
	_, _ = db.Exec(sql)

	result, err := db.Exec("select * from sqlite_master;")
	if err != nil {
		t.Fatal(err)
		return
	}
	fmt.Printf("%+v\n", result)

	fmt.Println("数据库连接成功")
}

func TestXorm(t *testing.T) {
	dbPath := filepath.Join(filepath.Dir(os.Args[0]), "test.db")
	fmt.Printf("dbPath => %v\n", dbPath)

	// 加密
	//dataSourceName := fmt.Sprintf("file:%s?_pragma_key=%s", dbPath, "shikong")
	// 不加密
	dataSourceName := fmt.Sprintf("file:%s?", dbPath)
	db, err := xorm.NewEngine("sqlite3", dataSourceName)
	if err != nil {
		t.Fatal(err)
		return
	}

	type SqliteMaster struct {
		Type      string `xorm:"type"`
		Name      string `xorm:"name"`
		TableName string `xorm:"tbl_name"`
		RootPage  int    `xorm:"rootpage"`
		Sql       string `xorm:"sql"`
	}
	all := make([]SqliteMaster, 0)

	_ = db.Table("sqlite_master").Find(&all)
	for _, item := range all {
		fmt.Printf("%+v\n", item)
	}
}
