package common

import (
	"encoding/json"
	"os"
)

type Config struct {
	FetchTokenUrl    string `json:"fetchTokenUrl"`
	Phone            string `json:"phone"`
	FetchDataUrl     string `json:"fetchDataUrl"`
	FetchData2Url    string `json:"fetchData2Url"`
	SendCodeUrl      string `json:"sendCodeUrl"`
	CheckSendCodeUrl string `json:"checkSendCodeUrl"`
	PhoneLoginUrl    string `json:"phoneLoginUrl"`
	MgrLoginUrl      string `json:"mgrLoginUrl"`
	ValidCodeUrl     string `json:"validCodeUrl"`
	StaffInfoUrl     string `json:"staffInfoUrl"`
	EnterpriseName   string `json:"enterpriseName"`
	Port             string `json:"port"`
	PushDataUrl      string `json:"pushDataUrl"`
	LocalDetectUrl   string `json:"localDetectUrl"`
	DevId            string `json:"devId"`
	DevIp            string `json:"devIp"`
	Timer            int    `json:"timer"`
	Sync             int    `json:"sync"`
	Mode             string `json:"mode"`
}

var ConfigData Config

func LoadConfig() interface{} {
	// 读取配置文件
	data, err := os.ReadFile("config.json")
	if err != nil {
		return nil
	}

	// 解析配置文件内容到全局变量中
	err = json.Unmarshal(data, &ConfigData)
	if err != nil {
		return nil
	}
	return ConfigData
}

func UpdateConfig(updateValues map[string]interface{}) string {

	config := ConfigData

	for key, value := range updateValues {
		switch key {
		case "port":
			config.Port = value.(string)
		case "phone":
			config.Phone = value.(string)
		case "devId":
			config.DevId = value.(string)
		case "devIp":
			config.DevIp = value.(string)
		case "mode":
			config.Mode = value.(string)
		case "enterpriseName":
			config.EnterpriseName = value.(string)
		case "fetchTokenUrl":
			config.FetchTokenUrl = value.(string)
		case "fetchDataUrl":
			config.FetchDataUrl = value.(string)
		case "fetchData2Url":
			config.FetchData2Url = value.(string)
		case "pushDataUrl":
			config.PushDataUrl = value.(string)
		case "sendCodeUrl":
			config.SendCodeUrl = value.(string)
		case "checkSendCodeUrl":
			config.CheckSendCodeUrl = value.(string)
		case "mgrLoginUrl":
			config.MgrLoginUrl = value.(string)
		case "phoneLoginUrl":
			config.PhoneLoginUrl = value.(string)
		case "localDetectUrl":
			config.LocalDetectUrl = value.(string)
		case "validCodeUrl":
			config.ValidCodeUrl = value.(string)
		case "staffInfoUrl":
			config.StaffInfoUrl = value.(string)
		case "timer":
			config.Timer = int(value.(float64))
		case "sync":
			config.Sync = int(value.(float64))
		default:
			return "Unknown config key: " + key
		}
	}

	if err := writeConfigToFile(&config, "config.json"); err != nil {
		return "Failed to write config to file: " + err.Error()
	}

	return ""

}

// 从文件中读取配置
func readConfigFromFile(filename string) (*Config, error) {
	fileContent, err := os.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	var config Config
	if err := json.Unmarshal(fileContent, &config); err != nil {
		return nil, err
	}

	return &config, nil
}

// 将配置写入文件
func writeConfigToFile(config *Config, filename string) error {
	configJSON, err := json.MarshalIndent(config, "", "  ")
	if err != nil {
		return err
	}

	return os.WriteFile(filename, configJSON, 0644)
}
