package com.wx.application.core.service;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wx.application.base.BaseService;
import com.wx.application.core.entity.OntologyField;
import com.wx.application.core.mapper.OntologyFieldMapper;
import com.wx.application.nebula.graph.bean.ModelVertex;
import com.wx.application.nebula.graph.bean.NebulaField;
import com.wx.application.nebula.graph.query.NebulaNode;
import com.wx.application.nebula.graph.service.NebulaModelService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author zj
 * @since 2022-09-25
 */
@Slf4j
@Service("coreOntologyFieldService")
@Transactional
public class OntologyFieldService extends BaseService<OntologyField, OntologyFieldMapper> {

	@Autowired
	OntologyFieldMapper baseMapper;
	
	@Autowired
	NebulaModelService nebulaModelService;
	
	public OntologyField createOntologyField(OntologyField ontologyField) {
		/**
		 * 验证label
		 */
		OntologyField has = baseMapper.findByOntologyIdAndLabel(ontologyField.getOntologyId(), 
				ontologyField.getField());
		
		if(has != null && has.getId() > 0) {
			return has;
		}
		
		return create(ontologyField);
	}
	
	/**
	 * 根据选择的概念添加属性
	 * @param OntologyField
	 */
	public void createConceptField(OntologyField ontologyField) {
		
		Long id = ontologyField.getId();
		
		OntologyField field = getOne(id);
		
		if(field == null) {
			return;
		}
		
		List<String> vids = ontologyField.getConceptIds();
		
		/**
		 * 先获得本体的所有概念
		 */
		List<NebulaNode> nodes = nebulaModelService.findNodesByOntologyId(field.getOntologyId());
		
		for(NebulaNode node : nodes) {
			
			List<NebulaField> fields = null;
			NebulaField nf = null;
			
			String operate = null;
			int findex = -1;
			
			JSONObject properties = node.getProperties();
			JSONArray fieldsnapshot = properties.getJSONArray("nebulafieldsnapshot");
			ModelVertex vertex = properties.toBean(ModelVertex.class);
			
			if(vids.contains(node.getVid())) {
				operate = "add";
			}
			
			/**
			 * 如果当前概念包括值属性
			 */
			if(fieldsnapshot != null) {
				fields = fieldsnapshot.toList(NebulaField.class);
				List<String> fieldnames = fields.stream().map(NebulaField::getField).collect(Collectors.toList());
				if(fieldnames.contains(field.getField())) {
					operate = vids.contains(node.getVid())?"have":"remove";
					findex = fieldnames.indexOf(field.getField());
				}
			}
			
			if("add".equals(operate) || "remove".equals(operate)) {
				if(fields == null) {
					fields = new ArrayList<>();
				}
				switch (operate) {
				case "add":
					BeanUtil.copyProperties(field, nf = new NebulaField());
					fields.add(nf);
					break;
				case "remove":
					if(findex > -1) {
						fields.remove(findex);
					}
					break;
				default:
					break;
				}
				vertex.setFields(fields);
				vertex.setVid(node.getVid());
				nebulaModelService.insertModelVertex(vertex);
			}
		}
	}
	
	
	public void deleteConceptField(Long id) {
		
		OntologyField field = getOne(id);
		
		if(field == null) {
			return;
		}
		/**
		 * 先获得本体的所有概念
		 */
		List<NebulaNode> nodes = nebulaModelService.findNodesByOntologyId(field.getOntologyId());
		
		List<NebulaField> fields = null;
		
		for(NebulaNode node : nodes) {
			
			int findex = -1;
			String operate = null;
			
			JSONObject properties = node.getProperties();
			JSONArray fieldsnapshot = properties.getJSONArray("nebulafieldsnapshot");
			ModelVertex vertex = properties.toBean(ModelVertex.class);
			
			/**
			 * 如果当前概念包括值属性
			 */
			if(fieldsnapshot != null) {
				fields = fieldsnapshot.toList(NebulaField.class);
				List<String> fieldnames = fields.stream().map(NebulaField::getField).collect(Collectors.toList());
				if(fieldnames.contains(field.getField())) {
					operate = "remove";
					findex = fieldnames.indexOf(field.getField());
				}
			}
			
			if(StringUtils.isNotBlank(operate)) {
				switch (operate) {
				case "remove":
					if(findex > -1) {
						fields.remove(findex);
					}
					break;
				default:
					break;
				}
				vertex.setFields(fields);
				vertex.setVid(node.getVid());
				nebulaModelService.insertModelVertex(vertex);
			}
			
		}
		remove(id);
	}
	
	public void modifyOntologyField(OntologyField ontologyField) {
		
		Long id = ontologyField.getId();
		
		OntologyField field = getOne(id);
		
		if(field == null) {
			return;
		}
		
		/**
		 * 先获得本体的所有概念
		 */
		List<NebulaNode> nodes = nebulaModelService.findNodesByOntologyId(field.getOntologyId());
		
		List<NebulaField> fields = null;
		
		for(NebulaNode node : nodes) {
			
			int findex = -1;
			String operate = null;
			
			JSONObject properties = node.getProperties();
			JSONArray fieldsnapshot = properties.getJSONArray("nebulafieldsnapshot");
			ModelVertex vertex = properties.toBean(ModelVertex.class);
			
			/**
			 * 如果当前概念包括值属性
			 */
			if(fieldsnapshot != null) {
				fields = fieldsnapshot.toList(NebulaField.class);
				List<String> fieldnames = fields.stream().map(NebulaField::getField).collect(Collectors.toList());
				if(fieldnames.contains(field.getField())) {
					operate = "modify";
					findex = fieldnames.indexOf(field.getField());
				}
			}
			
			if(StringUtils.isNotBlank(operate)) {
				switch (operate) {
				case "modify":
					if(findex > -1) {
						fields.remove(findex);
					}
					NebulaField nf = null;
					BeanUtil.copyProperties(ontologyField, nf = new NebulaField());
					fields.add(nf);
					break;
				default:
					break;
				}
				vertex.setFields(fields);
				vertex.setVid(node.getVid());
				nebulaModelService.insertModelVertex(vertex);
			}
			
		}
		
		modify(ontologyField);
	}
	
}
