package code_gen

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"git.qianqiusoft.com/qianqiusoft/light-apiengine-client/code_gen/apigen"
	"git.qianqiusoft.com/qianqiusoft/light-apiengine-client/code_gen/utils"
	"io/ioutil"
)

/*type ResponeResult struct {
	Code int32 `json:"code"`
	//描述
	Msg string `json:"msg"`
	//数据
	Data []GenerateResult `json:"data"`
}
type GenerateResult struct {
	Name    string `json:"name"`
	Content []byte `json:"content"`
	Type    string `json:"type"`
}*/

func DoGenerate(xmlfile string, isCompress bool) []byte {

	bs, _ := ioutil.ReadFile(xmlfile)

	//body := &bytes.Buffer{}
	//body.Write(bs)
	app := utils.XmlApplication{}

	err := xml.Unmarshal(bs, &app)
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}

	//fmt.Println(bs)

	res := utils.ResponeResult{Code: 200, Msg: ""}
	res.Data = []utils.GenerateResult{}

	tData := map[string]interface{}{}
	tData["app"] = app

	var gr *utils.GenerateResult
	for i := 0; i < len(app.Beans.BeanList); i++ {
		bean := app.Beans.BeanList[i]
		tData["data"] = bean

		gr = utils.NewResult(
			"model",
			fmt.Sprintf("%s/models/%s_gen.go", app.ApplicationName, utils.CamelizeStr(bean.Name, true)),
			apigen.GenBean,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)

		//	var b bytes.Buffer
		//	b.Write(result.Data[i].Content)
		//	unzip := unzipbytes(&b)
		//	result.Data[i].Content = unzip.Bytes()

		//model.Content = utils.ZipBytes(res.Data[i].Content)
	}
	for i := 0; i < len(app.Controllers.ControllerList); i++ {
		c := app.Controllers.ControllerList[i]
		tData["data"] = c

		gr = utils.NewResult(
			"controllers",
			fmt.Sprintf("%s/controllers/gen/%sController_gen.go", app.ApplicationName, utils.CamelizeStr(c.Name, true)),
			apigen.GenController,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)

		gr = utils.NewResult(
			"controllers",
			fmt.Sprintf("%s/controllers/partial/%s/%sController.go", app.ApplicationName, c.Dir, utils.CamelizeStr(c.Name, true)),
			apigen.GenPartial,
			tData,
			isCompress)
		//fmt.Println(string(gr.Content))
		res.Data = append(res.Data, *gr)

		gr = utils.NewResult(
			"routers",
			fmt.Sprintf("%s/routers/%s_gen.go", app.ApplicationName, c.Name),
			apigen.GenRouter,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)

		gr = utils.NewResult(
			"vue",
			fmt.Sprintf("%s/vue/api/modules/%s/index.js", app.ApplicationName, c.Name),
			apigen.GenVue,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)
	}

	gr = utils.NewResult(
		"routers",
		fmt.Sprintf("%s/routers/router_gen.go", app.ApplicationName),
		apigen.GenRouterGenGo,
		tData,
		isCompress)
	res.Data = append(res.Data, *gr)

	gr = utils.NewResult(
		"models",
		fmt.Sprintf("%s/models/model.go", app.ApplicationName),
		apigen.GenModelGo,
		tData,
		isCompress)
	res.Data = append(res.Data, *gr)

	for i := 0; i < len(app.Tables.TableList); i++ {
		table := app.Tables.TableList[i]
		tData["data"] = table

		gr = utils.NewResult(
			"models",
			fmt.Sprintf("%s/models/%s_gen.go", app.ApplicationName, utils.CamelizeStr(table.Name, true)),
			apigen.GenModel,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)

		gr = utils.NewResult(
			"sql",
			fmt.Sprintf("%s/sqlconfig/%s_controller/%s_page_count.tpl", app.ApplicationName, table.Name, table.Name),
			apigen.GenCount,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)

		gr = utils.NewResult(
			"sql",
			fmt.Sprintf("%s/sqlconfig/%s_controller/%s_page_select.tpl", app.ApplicationName, table.Name, table.Name),
			apigen.GenSelect,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)

		gr = utils.NewResult(
			"sql",
			fmt.Sprintf("%s/sqlconfig/%s/%s_gen.xml", app.ApplicationName, app.ApplicationName, table.Name),
			apigen.GenSql,
			tData,
			isCompress)
		res.Data = append(res.Data, *gr)
	}

	/*
		main := utils.GenerateResult{}
		main.Type = "main"
		main.Name ="main.go"

		apiTpl := utils.GenerateResult{}
		apiTpl.Type = "doc"
		apiTpl.Name ="doc/api.tpl"

		docker := utils.GenerateResult{}
		docker.Type = "ci"
		docker.Name ="Dockerfile"

		godep := utils.GenerateResult{}
		godep.Type = "ci"
		godep.Name ="godep.yaml"

		gitignore := utils.GenerateResult{}
		gitignore.Type = "ci"
		gitignore.Name =".gitignore"

		drone := utils.GenerateResult{}
		drone.Type = "ci"
		drone.Name =".drone.yml"

		vue := utils.GenerateResult{}
		vue.Type = "vue"
		vue.Name ="vue/api/modules/%s/index.js"

		charts := utils.GenerateResult{}
		charts.Type = "charts"
		charts.Name ="charts/values.yaml"

		chartsTmpl := utils.GenerateResult{}
		chartsTmpl.Type = "charts"
		chartsTmpl.Name ="charts/templates/config.yaml"
	*/

	out, err := json.Marshal(res)
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}
	return out
}
