package com.qiuguo.iot.third.service;

import com.qiuguo.iot.base.enums.ChinesePartSpeechEnum;
import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.system.SystemTalkAnswerConfigEntity;
import com.qiuguo.iot.data.service.system.SystemTalkAnswerConfigService;
import com.qiuguo.iot.third.nlp.INlp;
import com.qiuguo.iot.third.nlp.NlpKey;
import com.qiuguo.iot.third.nlp.action.Action;
import com.qiuguo.iot.third.nlp.action.ActionTime;
import com.qiuguo.iot.third.nlp.action.Actions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class NlpService {
    @Resource
    private LacNlpService liguoNlpService;

    @Resource
    private SystemTalkAnswerConfigService systemTalkAnswerConfigService;

    public Mono<Actions> getActionWithLacSingle(String text){
        //20230928日记，后期改成先匹配用户的设备名称，再找动词
        return liguoNlpService.geSingletNlp(text).map(nlp -> {
            Actions actions = new Actions();
            actions.setActions(new ArrayList<>());
            //nlp.getKeys().sort(Comparator.comparing(NlpKey::getType)); //解析,按照type从小到大排序
            Action action = null;
            String name = "";
                    /*new Action();

            action.setName(new ArrayList<>());
            action.setLbs(new ArrayList<>());
            action.setAsk(text);*/
            int a = 1, b = 1;
            List<SystemTalkAnswerConfigEntity> systemTalkAnswerConfigEntities = new ArrayList<>();
            SystemTalkAnswerConfigEntity lastSystemTalkAnswerConfigEntity = null;
            String lastName = null;
            for (NlpKey key : nlp.getKeys()
            ) {
                if(a + b == 2){
                    a = 0;
                    b = 0;
                    if(systemTalkAnswerConfigEntities.size() > 0){//指令按照优先级排序
                        systemTalkAnswerConfigEntities.sort(Comparator.comparing(SystemTalkAnswerConfigEntity::getKeyOrder));
                        lastSystemTalkAnswerConfigEntity = systemTalkAnswerConfigEntities.get(0);
                        action.setSystemTalkAnswerConfigEntity(lastSystemTalkAnswerConfigEntity);
                        action.setAction(action.getSystemTalkAnswerConfigEntity().getAskKey());
                        systemTalkAnswerConfigEntities.clear();
                    }else if(lastSystemTalkAnswerConfigEntity != null){
                        action.setSystemTalkAnswerConfigEntity(lastSystemTalkAnswerConfigEntity);
                        action.setAction(action.getSystemTalkAnswerConfigEntity().getAskKey());
                    }
                    if(StringUtils.isNotEmpty(name)){
                        action.getName().add(name);
                        lastName = name;
                    }else if(action != null && StringUtils.isNotEmpty(lastName)){
                            action.getName().add(lastName);

                    }
                    action = new Action();
                    name = "";
                    action.setName(new ArrayList<>());
                    action.setLbs(new ArrayList<>());
                    action.setAsk(text);
                    actions.getActions().add(action);
                }

                if(key.getType().equals(ChinesePartSpeechEnum.v.getCode())){
                    action.setAction(key.getKey());
                    SystemTalkAnswerConfigEntity entity = systemTalkAnswerConfigService.getSystemTalkWithKey(action.getAction());
                    if(entity != null){
                        systemTalkAnswerConfigEntities.add(entity);
                    }
                    a = 1;
                }else if(key.getType().equals(ChinesePartSpeechEnum.n.getCode())){
                    //匹配到关键词就不能作为名词加入，未匹配到的，分割前的都加入名词
                    SystemTalkAnswerConfigEntity entity = systemTalkAnswerConfigService.getSystemTalkWithKey(key.getKey());
                    if(entity != null){
                        systemTalkAnswerConfigEntities.add(entity);
                        if(StringUtils.isNotEmpty(name)){//已记录的加进去
                            //
                            entity = systemTalkAnswerConfigService.getSystemTalkWithKey(name);
                            if(entity != null){
                                systemTalkAnswerConfigEntities.add(entity);
                            }else{
                                action.getName().add(name);//加入分割前的名词
                                name = "";
                            }
                        }
                    }else{
                        name += key.getKey();
                    }
                    b = 1;

                }else if(key.getType().equals(ChinesePartSpeechEnum.m.getCode())){
                    action.setStatus(key.getKey());
                }else if(key.getType().equals(ChinesePartSpeechEnum.c.getCode())){
                    if(StringUtils.isNotEmpty(name)){
                        action.getName().add(name);
                    }
                    name = "";
                }else if(key.getType().equals(ChinesePartSpeechEnum.nt.getCode())){
                    action.getLbs().add(key.getKey());
                }else if(key.getType().equals(ChinesePartSpeechEnum.t.getCode())){
                    //解析时间关键字
                    action.setTime(new ActionTime());
                    action.getTime().setTime(key.getKey());
                }

            }
            if(StringUtils.isNotEmpty(name)){

                SystemTalkAnswerConfigEntity entity = systemTalkAnswerConfigService.getSystemTalkWithKey(name);
                if(entity != null){
                    systemTalkAnswerConfigEntities.add(entity);
                }else{
                    action.getName().add(name);
                }
            }else{
                if(StringUtils.isNotEmpty(lastName)){
                    action.getName().add(lastName);
                }
            }

            if(systemTalkAnswerConfigEntities.size() > 0){//指令按照优先级排序
                systemTalkAnswerConfigEntities.sort(Comparator.comparing(SystemTalkAnswerConfigEntity::getKeyOrder));
                action.setSystemTalkAnswerConfigEntity(systemTalkAnswerConfigEntities.get(0));
                action.setAction(action.getSystemTalkAnswerConfigEntity().getAskKey());
            }else if(lastSystemTalkAnswerConfigEntity != null){
                action.setSystemTalkAnswerConfigEntity(lastSystemTalkAnswerConfigEntity);
                action.setAction(action.getSystemTalkAnswerConfigEntity().getAskKey());
            }

            return actions;
        });
    }
}
