package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"gorm.io/gorm"
	"os"
	"strings"
	"time"
)

var (
	reader = bufio.NewReader(os.Stdin)
)

var statusMap = map[string]string{
	"queued":    "排队中",
	"running":   "任务运行中",
	"cancelled": "取消任务",
	"succeeded": "任务成功",
	"failed":    "任务失败",
}

type Conf struct {
	Dns   string `json:"dns"`
	Table string `json:"table"`
}

type Video struct {
	Url  string `json:"url"`
	Text string `json:"text"`
}

var (
	c  *Conf
	db *gorm.DB
)

func main() {
	log("正在读取配置。。。")
	dir, err := os.Getwd()
	if err != nil {
		panic("获取当前路径失败")
	}
	c = getConf(dir)
	success("读取配置成功！请务必保证网络通畅")
	sqlc, err := setDb()
	if err != nil {
		fatal("数据库连接失败1:%v", err)
	}
	defer sqlc.Close()

	do(dir)

}
func do(dir string) {
	csvData, title, files := getCsvInfo(dir)
	set := setOp(title)
	cutChannel := cutData(csvData, 1)
	saveData(cutChannel, set, title, dir)
	overFiles(files)
	finish(dir)
	select {}
}

func getConf(dir string) *Conf {

	fp := fmt.Sprintf("%s/%s", dir, "import_csv_data")
	file, err := os.Open(fp)
	if err != nil {
		if !os.IsNotExist(err) {
			os.Remove(fp)
		}
		return cConf(fp)
	}
	defer file.Close()
	content, err := os.ReadFile(fp)
	if err != nil {
		fatal("获取配置信息失败")
		exit()
	}

	var (
		pwd  string
		conf = new(Conf)
	)
	for {
		input("请输入访问密码: ")
		pwd, _ = reader.ReadString('\n')
		cjson, err := decrypt([]byte(padToLength(strings.TrimSpace(pwd), 16, ' ')), string(content))
		if err != nil {
			warning("密码错误")
			continue
		}
		err = json.Unmarshal([]byte(cjson), &conf)
		if err != nil {
			warning("配置读取失败")
		}
		break
	}
	return conf
}

func cConf(fp string) *Conf {
	conf := new(Conf)
	log("进入初始化配置向导")
	log("----------------------")

	input("请设置DNS: ")
	k, _ := reader.ReadString('\n')
	conf.Dns = strings.TrimSpace(k)

	input("请设置数据表: ")
	t, _ := reader.ReadString('\n')
	conf.Table = strings.TrimSpace(t)

	var (
		pwd string
	)
	for {
		input("请设置访问密码(密码长度请小于16位): ")
		pwd, _ = reader.ReadString('\n')
		if len(strings.TrimSpace(pwd)) > 16 {
			warning("密码长度请小于16位")
			continue
		}
		break
	}
	cjson, _ := json.Marshal(conf)
	pad := padToLength(strings.TrimSpace(pwd), 16, ' ')
	en, e := encrypt([]byte(pad), string(cjson))
	if e != nil {
		fatal("设置失败，程序退出:%v", e)
		exit()
	}
	if err := os.WriteFile(fp, []byte(en), 0666); err != nil {
		fatal("写入失败，程序退出")
		exit()
	}
	return conf

}

func exit() {
	waitTime := 3
	warning("程序即将再%d秒后退出", waitTime)
	for {
		if waitTime == 0 {
			os.Exit(1)
		}
		time.Sleep(1 * time.Second)
		warning("%d", waitTime)
		waitTime--
	}

}

func finish(dir string) {
	do(dir)
}

func overFiles(files []string) {
	for _, v := range files {
		err := os.Rename(v, fmt.Sprintf("已导入_%s", v))
		if err != nil {
			warning("重命名文件时出错: %v\n", err)
			exit()
		}
	}
}
