package templategen

import (
	"github.com/Pena-Co-Ltd/amocrm_templategen_back/dal/model"
	"github.com/Pena-Co-Ltd/amocrm_templategen_back/gdisk"
)

func GDiskGenerateDoc(template *model.Template, saveFolderID string, client *GDisk.Client, data interface{}) error {
	filename := GenerateDocName(template.Name, template.UserID)

	// Download file
	downloaded := TempDownloaded + "/" + filename
	err := client.DownloadFile(downloaded, template.FileID)
	if err != nil {
		return err
	}

	// Generate file
	generated := TempGenerated + "/" + filename
	err = GenerateFile(filename, data)
	if err != nil {
		return err
	}

	// Upload file
	err = client.UploadFile(
		TempGenerated+"/"+filename,
		"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		saveFolderID)

	// Delete temps
	err = DeleteDocument(downloaded)
	if err != nil {
		return err
	}

	err = DeleteDocument(generated)

	return err
}
