package config

import (
	"eta/eta_mini_ht_api/common/contants"
	"fmt"
	"strings"
	"sync"
)

var (
	fileOnce   sync.Once
	fileConfig *FileConfig
)

type FileOpts struct {
	Path       string
	Notice     string
	Disclaimer string
	PublicKey  string
}
type FileConfig struct {
	BaseConfig
	opts FileOpts
}

func (f *FileConfig) GetNotice() string {
	if strings.HasSuffix(f.opts.Path, "/") {
		return fmt.Sprint(f.opts.Path, f.opts.Notice)
	}
	return fmt.Sprint(f.opts.Path, "/", f.opts.Notice)
}
func (f *FileConfig) GetPath() string {
	return f.opts.Path
}
func (f *FileConfig) GetPublicKey() string {
	return f.opts.PublicKey
}
func (f *FileConfig) GetDisclaimer() string {
	if strings.HasSuffix(f.opts.Path, "/") {
		return fmt.Sprint(f.opts.Path, f.opts.Disclaimer)
	}
	return fmt.Sprint(f.opts.Path, "/", f.opts.Disclaimer)
}
func (f *FileConfig) InitConfig() {
	opts := FileOpts{
		Path:       f.GetString("path"),
		Notice:     f.GetString("notice"),
		Disclaimer: f.GetString("disclaimer"),
		PublicKey:  f.GetString("publicKey"),
	}
	f.opts = opts
}
func NewFileConfig() Config {
	if fileConfig == nil {
		fileOnce.Do(func() {
			fileConfig = &FileConfig{
				BaseConfig: BaseConfig{prefix: contants.FILE},
				opts:       FileOpts{},
			}
		})
	}
	return fileConfig
}

func init() {
	Register(contants.FILE, NewFileConfig)
}