/**
 * Copyright (C) 2014 Deepin Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 **/

package main

import (
	"fmt"
	"pkg.deepin.io/lib/dbus"
	dutils "pkg.deepin.io/lib/utils"
)

const (
	defaultLocaleFile    = "/etc/default/locale"
	defaultLocaleGenFile = "/etc/locale.gen"
)

func (h *Helper) SetLocale(locale string) error {
	if !IsLocaleValid(locale) {
		return fmt.Errorf("Invalid locale:", locale)
	}

	return writeContentToFile(defaultLocaleFile,
		fmt.Sprintf("LANG=%s", locale))
}

func (h *Helper) GenerateLocale(locale string) error {
	h.running = true
	defer func() {
		h.running = false
	}()

	if !IsLocaleValid(locale) {
		dbus.Emit(h, "Success", false,
			fmt.Sprintf("Invalid locale: %v", locale))
		return fmt.Errorf("Invalid locale:", locale)
	}

	// locales version <= 2.13
	if !dutils.IsFileExist(defaultLocaleGenFile) {
		err := h.doGenLocaleWithParam(locale)
		if err != nil {
			dbus.Emit(h, "Success", false, err.Error())
			return err
		}

		dbus.Emit(h, "Success", true, "")
		return nil
	}

	err := enableLocaleInFile(locale, defaultLocaleGenFile)
	if err != nil {
		dbus.Emit(h, "Success", false, err.Error())
		return err
	}

	err = h.doGenLocale()
	if err != nil {
		dbus.Emit(h, "Success", false, err.Error())
		return err
	}

	dbus.Emit(h, "Success", true, "")
	return nil
}

func enableLocaleInFile(locale, file string) error {
	finfo, err := NewLocaleFileInfo(file)
	if err != nil {
		return err
	}

	if finfo.IsLocaleEnabled(locale) {
		return nil
	}

	finfo.EnableLocale(locale)
	err = finfo.Save(defaultLocaleGenFile)
	if err != nil {
		return err
	}

	return nil
}
