package com.sooka.component.beetl.tag.cms;

import com.github.pagehelper.PageInfo;
import com.sooka.common.utils.DateUtil;
import com.sooka.module.web.cms.service.CategoryService;
import com.sooka.module.web.cms.service.SiteService;
import com.sooka.mybatis.model.TCmsCategory;
import com.sooka.mybatis.model.TCmsSite;
import com.sooka.common.exception.CmsException;
import com.sooka.common.exception.SystemException;
import com.sooka.common.utils.CmsUtil;
import com.sooka.common.utils.StrUtil;
import com.sooka.module.web.cms.service.ContentService;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description:内容列表标签
 *
 * @author Jin
 * @create 2017-05-26
 **/
@Service
@Scope("prototype")
public class ContentListTag extends GeneralVarTagBinding {

    @Autowired
    private ContentService contentService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SiteService siteService;

    @Value("${system.http.protocol}")
    private String httpProtocol;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Value("${system.http.host}")
    private String httpHost;

    @Value("${system.site.prefix}")
    private String sitePrefix;


    /**
     * 文章列表标签
     *
     * siteId:站点id
     * categoryId:分类编号
     * hasChild：是否包含子栏目内容
     * isHot：热门
     * titleLen:标题长度
     * target:是否新窗口打开
     * orderBy:排序
     * size:调用条数
     * recommend:是否推荐 ：0为不推荐，1为推荐
     *
     */
    @Override
    public void render() {
        if (CmsUtil.isNullOrEmpty(this.args[1]))
            throw  new SystemException("标签参数不能为空！");
        Integer titleLen =  Integer.parseInt((String) this.getAttributeValue("titleLen"));
        Integer titlesize= null;
        try {
            titlesize =  Integer.parseInt((String) this.getAttributeValue("titleSize"));
        } catch (NumberFormatException e) {
        }
        Integer siteId=  (this.getAttributeValue("siteId") instanceof String)?Integer.parseInt((String) this.getAttributeValue("siteId")):(Integer)this.getAttributeValue("siteId");
        Long categoryId=  (this.getAttributeValue("categoryId") instanceof String)?Long.parseLong((String) this.getAttributeValue("categoryId")):(Long) this.getAttributeValue("categoryId");
        Integer hasChild=  Integer.parseInt((String) this.getAttributeValue("hasChild"));
        Integer isPic =  Integer.parseInt(CmsUtil.isNullOrEmpty(this.getAttributeValue("isPic"))?"3":(String)this.getAttributeValue("isPic"));
        Integer isRecommend =  Integer.parseInt(CmsUtil.isNullOrEmpty(this.getAttributeValue("isRecommend"))?"0":(String) this.getAttributeValue("isRecommend"));
        Integer orderBy =  Integer.parseInt((String) this.getAttributeValue("orderBy"));
        Integer pageNumber=null;
        try {
            pageNumber =  Integer.parseInt((CmsUtil.isNullOrEmpty(this.getAttributeValue("pageNumber"))?"1":(String) this.getAttributeValue("pageNumber")));
        }catch (Exception e){
            pageNumber=Integer.parseInt(this.getAttributeValue("pageNumber").toString());
        }
        Integer pageSize =  Integer.parseInt((String) this.getAttributeValue("size"));
        Integer isHot =  Integer.parseInt((String) this.getAttributeValue("isHot"));
        PageInfo<Map> pageInfo = contentService.findContentListBySiteIdAndCategoryId(siteId, categoryId, orderBy, pageNumber,pageSize, hasChild, isHot, isPic,isRecommend);
        if(CmsUtil.isNullOrEmpty(pageInfo.getList())) return;
        this.wrapRender(pageInfo.getList(),titleLen,siteId,titlesize);

    }

    private void wrapRender(List<Map>  contentList, int titleLen, int siteId,Integer titleSize) {
        int i = 1;
        for (Map content : contentList) {
            //add categoryName
            Long categoryId = Long.parseLong(content.get("categoryId").toString());
            TCmsCategory category = categoryService.findById(categoryId);
            if(category != null){
                content.put("categoryName",category.getCategoryName());
            }
            String title = content.get("title").toString();
            int length = title.length();

            if (titleSize != null){
                if (length > titleSize) {
                    content.put("titleSize",title.substring(0, titleSize) + "...");
                }
                String contenPicture = (String) content.get("content");
                int src = contenPicture.indexOf("src=\"");
                src = src + 5;
                String substring = contenPicture.substring(src);
                String substring1 = null;
                try {
                    substring1 = substring.substring(0,substring.indexOf("\""));
                } catch (Exception e) {
                    substring1="";
                }
                content.put("src",substring1);
            }


            if (length > titleLen) {
                content.put("title",title.substring(0, titleLen) + "...");
            }
            if (StrUtil.isBlank(content.get("url").toString())) {
                TCmsSite site = siteService.findById(siteId);
                if(CmsUtil.isNullOrEmpty(site)) throw new CmsException("站点不存在[siteId:"+siteId+"]");
                String url = httpProtocol + "://" + (StrUtil.isBlank(site.getDomain())?httpHost:site.getDomain()) + "/"+sitePrefix+"/"+site.getSiteId()+"/";
                url+=content.get("categoryId")+"/"+content.get("contentId");
                content.put("url",url+siteSubfix);
            }

            content.put("index",i);
            int currentTime = DateUtil.differentDaysByMillisecond((Date) content.get("inputdate"), new Date());
            if(currentTime <=5){
                content.put("newRelease",true);
            }else {
                content.put("newRelease",false);
            }
            this.binds(content);
            this.doBodyRender();
            i++;
        }

    }

}
