package com.publiccms.views.directive.tools;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.common.handler.RenderHandler;
import com.publiccms.common.tools.CmsLangUtils;
import com.publiccms.common.tools.CommonUtils;
import com.publiccms.entities.cms.CmsCategory;
import com.publiccms.entities.cms.CmsCategoryLang;
import com.publiccms.entities.cms.CmsCategoryLangId;
import com.publiccms.entities.sys.SysSite;
import com.publiccms.logic.component.config.SiteAttributeComponent;
import com.publiccms.logic.component.template.TemplateComponent;
import com.publiccms.logic.service.cms.CmsCategoryLangService;
import com.publiccms.logic.service.cms.CmsCategoryService;

import freemarker.template.TemplateException;

/**
 *
 * createCategoryFile 创建分类静态文件指令
 * <p>
 * 参数列表
 * <ul>
 * <li><code>id</code>:分类id
 * <li><code>templatePath</code>:模板路径
 * <li><code>filePath</code>:静态文件路径
 * <li><code>pageIndex</code>:当前页码,默认为1
 * <li><code>totalPage</code>:最大页码,为空时则只生成当前页
 * </ul>
 * <p>
 * 返回结果
 * <ul>
 * <li><code>url</code>:静态文件路径
 * </ul>
 * <p>
 * 使用示例
 * <p>
 * &lt;@tools.createCategoryFile id=1 templatePath='category.html'
 * filePath='category/'+1+'.html'&gt;${url}&lt;/@tools.createCategoryFile&gt;
 *
 * <pre>
&lt;script&gt;
 fetch('${site.dynamicPath}api/directive/tools/createCategoryFile?id=1&amp;templatePath=category.html&amp;filePath=category/1.html',{"headers":{"appToken":"接口访问授权Token"}}).then(res => res.json()).then(data=>{
   console.log(data);
 });
 &lt;/script&gt;
 * </pre>
 */
@Component
public class CreateCategoryFileDirective extends AbstractTemplateDirective {

    @Override
    public void execute(RenderHandler handler) throws IOException, TemplateException {
        Integer id = handler.getInteger("id");
        String lang = handler.getStringAttribute("lang");
        String templatePath = handler.getString("templatePath");
        String filepath = handler.getString("filePath");
        Integer pageIndex = handler.getInteger("pageIndex");
        if (CommonUtils.notEmpty(id) && CommonUtils.notEmpty(templatePath) && CommonUtils.notEmpty(filepath)) {
            SysSite site = getSite(handler);
            try {
                CmsCategory category = categoryService.getEntity(id);
                if (null != category && site.getId() == category.getSiteId()) {
                    CmsCategoryLang langEntity = null;
                    if (CommonUtils.notEmpty(lang) && !lang.equalsIgnoreCase(category.getLang())) {
                        langEntity = categoryLangService.getEntity(new CmsCategoryLangId(id, lang));
                        String defaultLang = siteAttributeComponent.getDefaultLanguage(site.getId());
                        CmsLangUtils.initLang(category, defaultLang, langEntity);
                    }
                    handler.put("url", templateComponent.createCategoryFile(site, category, langEntity, templatePath, filepath,
                            pageIndex, handler.getInteger("totalPage"))).render();
                }
            } catch (IOException | TemplateException e) {
                handler.print(e.getMessage());
            }
        }
    }

    @Override
    public boolean needAppToken() {
        return true;
    }

    @Resource
    private TemplateComponent templateComponent;
    @Resource
    private CmsCategoryService categoryService;
    @Resource
    private CmsCategoryLangService categoryLangService;
    @Resource
    protected SiteAttributeComponent siteAttributeComponent;
}
