package com.qiuguo.iot.third.service;

import com.qiuguo.iot.base.enums.ChinesePartSpeechEnum;
import com.qiuguo.iot.base.enums.DeviceTypeEnum;
import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceUserBindEntity;
import com.qiuguo.iot.data.entity.system.SystemTalkAnswerConfigEntity;
import com.qiuguo.iot.data.request.device.DeviceUserBindRequest;
import com.qiuguo.iot.data.service.device.DeviceUserBindService;
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.*;

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

    @Resource
    private DeviceUserBindService deviceUserBindService;

    @Resource
    private SystemTalkAnswerConfigService systemTalkAnswerConfigService;

    /**
     * 查询最多设备数量
     */
    private static Integer MAX_COUT = 2000;

    public Mono<Actions> getActionWithLacSingle(Long userId, String text){
        /*if(userId != null){
            DeviceUserBindRequest request = new DeviceUserBindRequest();
            request.setUserId(userId);
            //涂鸦设备
            request.setDeviceType(DeviceTypeEnum.ELECTRIC_SOCKET.getCode());
            request.setPageSize(MAX_COUT);
            return deviceUserBindService.selectDeviceUserBindsByRequest(request).flatMap(deviceUserBindEntityPagerResult -> {
                if(deviceUserBindEntityPagerResult.getData().size() > 0){
                    List<DeviceUserBindEntity> list = deviceUserBindEntityPagerResult.getData();
                    list.sort((a, b) -> {
                        return b.getBindName().length() - a.getBindName().length();
                    });//按照名称由长到短排序
                    List<DeviceUserBindEntity> includs = new ArrayList<>();
                    String pText = text;
                    int i = 0;
                    for (DeviceUserBindEntity entity:list
                         ) {
                        if(pText.indexOf(entity.getBindName()) >= 0){
                            //找到对应的名称
                            pText = pText.replace(entity.getBindName(), ">" + (i++) + "<");
                            includs.add(entity);

                        }
                    }


                    if(includs.size() > 0){
                        //String ppText = pText;
                        return liguoNlpService.geSingletNlp(pText).map(nlp -> {
                            Actions actions = new Actions();
                            actions.setActions(new ArrayList<>());
                            //nlp.getKeys().sort(Comparator.comparing(NlpKey::getType)); //解析,按照type从小到大排序
                            Action action = new Action();
                            String name = "";


                            action.setName(new ArrayList<>());
                            action.setLbs(new ArrayList<>());
                            action.setAsk(text);
                            int a = 0, b = 0;
                            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);

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

                                }

                                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());
                                }else if(key.getType().equals(ChinesePartSpeechEnum.qg.getCode())){
                                    //替换成对应的设备

                                }

                            }

                            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());
                                //actions.getActions().add(action);
                            }else if(lastSystemTalkAnswerConfigEntity != null){
                                action.setSystemTalkAnswerConfigEntity(lastSystemTalkAnswerConfigEntity);
                                action.setAction(action.getSystemTalkAnswerConfigEntity().getAskKey());
                                //actions.getActions().add(action);
                            }
                            if(a + b > 0){
                                actions.getActions().add(action);
                            }


                            return actions;
                        });
                    }

                }
                //Actions actions = null;
                return getActions(text);
            });
        }*/

        //20230928日记，后期改成先匹配用户的设备名称，再找动词
        return getActions(text);
    }

    public Mono<Actions> getActions(String text) {
        return liguoNlpService.geSingletNlp(text).map(nlp -> {
            Actions actions = new Actions();
            actions.setActions(new ArrayList<>());
            //nlp.getKeys().sort(Comparator.comparing(NlpKey::getType)); //解析,按照type从小到大排序
            Action action = new Action();
            String name = "";


            action.setName(new ArrayList<>());
            action.setLbs(new ArrayList<>());
            action.setAsk(text);
            int a = 0, b = 0;
            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);

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

                }

                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());
                //actions.getActions().add(action);
            }else if(lastSystemTalkAnswerConfigEntity != null){
                action.setSystemTalkAnswerConfigEntity(lastSystemTalkAnswerConfigEntity);
                action.setAction(action.getSystemTalkAnswerConfigEntity().getAskKey());
                //actions.getActions().add(action);
            }
            if(a + b > 0){
                actions.getActions().add(action);
            }


            return actions;
        });
    }
}
