/*
   Nging is a toolbox for webmasters
   Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

package setup

import (
	"errors"

	"github.com/coscms/webcore/library/backend"
	"github.com/coscms/webcore/library/config"
	"github.com/coscms/webcore/library/license"
	"github.com/webx-top/com"
	"github.com/webx-top/echo"
	"github.com/webx-top/echo/code"
)

func postLicense(c echo.Context) error {
	content := c.Formx(`license`).String()
	if len(content) == 0 {
		return c.NewError(code.DataUnavailable, `授权文件内容不能为空`).SetZone(`license`)
	}
	b := com.Str2bytes(content)
	err := license.VerifyPostLicenseContent(c, b)
	if err != nil {
		return err
	}

	err = license.Save(b)
	return err
}

// License 获取商业授权
func License(c echo.Context) error {
	if c.Queryx(`jumpToProduct`).Bool() { // {{BackendURL}}/license?jumpToProduct=1&op=buy
		productURL := license.ProductDetailURL()
		args := []string{}
		op := c.Query(`op`)
		if len(op) > 0 {
			args = append(args, `op`, op)
		}
		productURL = com.WithURLParams(productURL, `source`, c.Site(), args...)
		return c.Redirect(productURL)
	}
	err := license.Check(c)
	if err != nil && c.IsPost() {
		err = postLicense(c)
	}
	if err == nil {
		nextURL := echo.GetNextURL(c)
		if len(nextURL) == 0 {
			nextURL = backend.URLFor(`/`)
		}
		config.Version.Licensed = true
		return c.Redirect(nextURL)
	}
	errMap := map[string]string{
		`Could not read license`:        c.T(`读取授权文件失败`),
		`Could not read private key`:    c.T(`读取私钥失败`),
		`Could not read public key`:     c.T(`读取公钥失败`),
		`Could not read machine number`: c.T(`获取机器码失败`),
		`Invalid private key`:           c.T(`私钥无效`),
		`Invalid public key`:            c.T(`公钥无效`),
		`Invalid License file`:          c.T(`授权文件无效`),
		`Unlicensed Version`:            c.T(`未授权版本`),
		`Invalid MachineID`:             c.T(`机器码无效`),
		`Invalid LicenseID`:             c.T(`授权ID无效`),
		`License expired`:               c.T(`授权已过期`),
		`License does not exist`:        c.T(`授权文件不存在`),
	}
	if errStr, ok := errMap[err.Error()]; ok {
		err = errors.New(errStr)
	}
	//需要重新获取授权文件
	if err == license.ErrLicenseNotFound && license.LicenseMode() != license.ModeDomain {
		err = license.DownloadOnce(c)
		c.Set(`downloaded`, err == nil)
	} else {
		c.Set(`downloaded`, false)
	}

	c.Set(`licenseFile`, license.FilePath())
	c.Set(`fileName`, license.FileName())
	return c.Render(`license`, err)
}
