package com.softwarebr.nunu.service.go.impl;

import com.brframework.commoncms.core.cms.Select;
import com.brframework.commoncms.core.cms.Selector;
import com.brframework.commonweb.core.SpringContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.softwarebr.nunu.config.SelectConfig;
import com.softwarebr.nunu.entity.go.Label;
import com.softwarebr.nunu.dao.go.LabelDao;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.go.LabelService;
import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;

/**
 * @author lilin
 * @date 2020-02-19 14:03:47
 */

@Slf4j
@Service
public class LabelServiceImpl extends AbstractEntityService<Label, Long, Object> implements LabelService, Selector<String, Select> {

    @Autowired
    private LabelDao labelDao;

    public LabelService getProxy() {
        if (AopContext.currentProxy() instanceof LabelService) {
            return (LabelService) AopContext.currentProxy();
        } else {
            return this;
        }
    }

    @Override
    public CommonRepository<Long, Label> getRepository() {
        return labelDao;
    }

    @Override
    public List<Label> getLabel(Long parentId) {
        return labelDao.findByParentIdAndStatusOrderBySortAsc(parentId, SystemConst.MARKER_STATUS_NO);
    }

    @Override
    public List<Label> getAll() {
        return labelDao.findByStatusOrderBySortAsc(SystemConst.MARKER_STATUS_NO);
    }

    @Override
    public Map<String, String> getLabel(String key) {
        List<Label> labels;
        if (StringUtils.isEmpty(key) || Selector.CMS_EMPTY_TYPE.equals(key)) {
            labels = getLabel(0L);
        } else {
            labels = getLabel(Long.parseLong(key));
        }
        Map<String, String> map = Maps.newHashMap();
        for (Label label : labels) {
            map.put(label.getId() + "", label.getLabelName());
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveLabel(Long parentId, String labelName, Integer sort) {
        Label label = new Label();
        label.setLabelName(labelName);
        label.setCreateDate(LocalDateTime.now());
        label.setParentId(parentId);
        label.setSort(sort);
        label.setStatus(SystemConst.MARKER_STATUS_NO);
        labelDao.save(label);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLabel(Long labelId, Long parentId, String labelName, Integer sort) {
        Label label = accessObject(labelId);
        label.setLabelName(labelName);
        label.setParentId(parentId);
        label.setSort(sort);
        labelDao.save(label);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeLabel(Long labelId) {
        Label label = accessObject(labelId);
        label.setStatus(SystemConst.MARKER_STATUS_YES);
        labelDao.save(label);
    }

    @Override
    public Map<String, Select> getConfig() {
        Map<String, Select> selector = Maps.newHashMap();
        Select label = this::getLabel;
        selector.put(SelectConfig.SELECT_LABEL, label);
        return selector;
    }
}