package cn.com.countrygarden.bi.main.service.wcx.impl;

import cn.com.countrygarden.bi.main.constant.wcx.WcxConstant;
import cn.com.countrygarden.bi.main.dao.wcx.WcxReviewMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReviewDto;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.wcx.WcxReview;
import cn.com.countrygarden.bi.main.service.wcx.WcxReviewService;
import cn.com.countrygarden.bi.main.util.PageUtil;
import cn.com.countrygarden.bi.main.util.wcx.CommonUtil;
import com.github.pagehelper.PageRowBounds;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Service
public class WcxReviewServiceImpl implements WcxReviewService {
    @Autowired
    private WcxReviewMapper wcxReviewMapper;


    @Override
    public boolean save(WcxReviewDto dto) {
        String reviewId = CommonUtil.getUUID();
        WcxReview wcxReview = new WcxReview(
                reviewId, dto.getReviewContent(),
                dto.getUserId(),dto.getInnovationId(),
                WcxConstant.STATUS_ENABLE, dto.getCreateDate());
        wcxReviewMapper.insert(wcxReview);
        return true;
    }

    @Override
    public PageData selectPageList(WcxReviewDto dto) throws UnsupportedEncodingException {
        int pageOffset = PageUtil.getPageOffset(dto.getPageIndex(), dto.getPageSize());
        dto.setPageOffset(pageOffset);

        List<WcxReviewDto> data = wcxReviewMapper.selectPageData(dto);
        for (WcxReviewDto reviewDto:data){
           // String result = EmojiParser.parseToAliases(reviewDto.getReviewContent());
            //reviewDto.setReviewContent(result);
/*            Pattern emoji = Pattern.compile ("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",Pattern.UNICODE_CASE | Pattern . CASE_INSENSITIVE ) ;
            Matcher emojiMatcher = emoji.matcher(reviewDto.getReviewContent());
            String result=reviewDto.setReviewContent(emojiMatcher.replaceAll("*"));*/
            String result= EmojiParser.parseToUnicode(reviewDto.getReviewContent());
            reviewDto.setReviewContent(result);
            System.out.println(reviewDto.getReviewContent());
        }
        Integer total = wcxReviewMapper.selectPageCount(dto);

        PageRowBounds pageRowBounds = new PageRowBounds(dto.getPageIndex(), dto.getPageSize());
        pageRowBounds.setTotal(total.longValue());
        pageRowBounds.setCount(true);

        PageData pageData = new PageData(data, pageRowBounds);
        return pageData;
    }

    @Override
    public boolean delete(String reviewId) {
        wcxReviewMapper.deleteByPrimaryKey(reviewId);
        return false;
    }
}
