package com.YuyeTech.TPlat.service.impl;

import com.YuyeTech.TPlat.dataobject.*;
import com.YuyeTech.TPlat.dto.LevelConditionDTO;
import com.YuyeTech.TPlat.dto.LevelHonorDTO;
import com.YuyeTech.TPlat.dto.LevelJumpDTO;
import com.YuyeTech.TPlat.repository.*;
import com.YuyeTech.TPlat.service.GameTagService;
import com.YuyeTech.TPlat.service.LevelJumpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


/**
 * @author:slambb
 * @date:2021/8/4
 */
@Service
public class LevelJumpServiceImpl implements LevelJumpService {

    @Autowired
    private LevelJumpRepository levelJumpRepository;
    @Autowired
    private LevelConditionRepository levelConditionRepository;
    @Autowired
    private LevelHonorRepository levelHonorRepository;

    @Autowired
    private UserLevelRepository userLevelRepository;

    @Override
    public List<LevelJump> findLevelJumpsByNumberType(Integer numberType,Sort.Direction direction) {
        return  levelJumpRepository.findAllByNumberType(numberType, Sort.by(direction,"sort"));
    }

    @Override
    public List<LevelJump> findLevelJumpsAll() {
        return levelJumpRepository.findAll();
    }

    @Override
    public List<LevelJumpDTO> findLevelJumpDTOsByNumberType(Integer numberType,Sort.Direction direction) {

        List<LevelJump> levelJumpList = levelJumpRepository.findAllByNumberType(numberType, Sort.by(direction,"sort"));
        List<LevelJumpDTO> levelJumpDTOList = levelJumpList.stream().map(
                e -> new LevelJumpDTO(
                        e.getId(),
                        e.getName(),
                        e.getDescribe(),
                        e.getRewardGold(),
                        e.getRewardDiamond(),
                        levelHonorRepository.findByType(e.getRewardHonor()).orElse(null),
                        e.getConsumeGold(),
                        e.getConsumeDiamond(),
                        e.getSignCount(),
                        e.getSignType(),
                        e.getLimitTime(),
                        levelConditionRepository.findByTypeAndLimitTypeIn(0, Pattern.compile(",").splitAsStream(e.getConditionUnlock()).map((x) -> Integer.parseInt(x))
                                .collect(Collectors.toList())),//解析关卡
                        levelConditionRepository.findByTypeAndLimitType(1, e.getConditionPassed()),
                        e.getExplain(),
                        e.getNumberType(),
                        false,
                        false
                )
        ).collect(Collectors.toList());

        return levelJumpDTOList;
    }

    @Override
    public Map findLevelJumpsAll(String userId) {

        //关卡解锁数据读取，输出一个匹配的关卡数据
        UserLevel userLevel = userLevelRepository.findByUserIdAndEventType(userId, 1).orElse(null);
        List<Integer> unlockStateList = new ArrayList<>();
        List<Integer> passedStateList = new ArrayList<>();

        if(userLevel !=null){
            // 记录一个 id 数组。
            // 已经解锁的关卡id "0,1,2,3"
            unlockStateList = Pattern.compile(",").splitAsStream(userLevel.getUnlockState()).map((x) -> Integer.parseInt(x))
                    .collect(Collectors.toList());
            // 已经通过关卡的关卡id "0,1,2"
            passedStateList = Pattern.compile(",").splitAsStream(userLevel.getPassedState()).map((x) -> Integer.parseInt(x))
                    .collect(Collectors.toList());
        }

        List<LevelJump> levelJumpList = levelJumpRepository.findAll(Sort.by(Sort.Direction.ASC,"sort"));
        List<LevelJumpDTO> singlePerson = new ArrayList<>();
        List<LevelJumpDTO> multiPerson = new ArrayList<>();

        for (int i = 0; i < levelJumpList.size(); i++) {
            LevelJump e = levelJumpList.get(i);
            List<Integer> _limitTypes = Pattern.compile(",").splitAsStream(e.getConditionUnlock()).map((x) -> Integer.parseInt(x))
                    .collect(Collectors.toList());
            // 是否解锁,如果是 0 ，及存在自动解锁。
            Boolean _unlock = _limitTypes.contains(0);
            // 如果存在解锁关卡，并且不是1也是可以的
            Boolean _hasUnlock = unlockStateList.contains(e.getId());
            Boolean _isPassed = passedStateList.contains(e.getId());
            LevelJumpDTO _levelJumpDTO = new LevelJumpDTO(
                    e.getId(),
                    e.getName(),
                    e.getDescribe(),
                    e.getRewardGold(),
                    e.getRewardDiamond(),
                    levelHonorRepository.findByType(e.getRewardHonor()).orElse(null),
                    e.getConsumeGold(),
                    e.getConsumeDiamond(),
                    e.getSignCount(),
                    e.getSignType(),
                    e.getLimitTime(),
                    levelConditionRepository.findByTypeAndLimitTypeIn(0, _limitTypes),//解析关卡
                    levelConditionRepository.findByTypeAndLimitType(1, e.getConditionPassed()),
                    e.getExplain(),
                    e.getNumberType(),
                    _isPassed,
                    _unlock || _hasUnlock
            );

            //单人，多人
            if (e.getNumberType().equals(1)) {
                singlePerson.add(_levelJumpDTO);
            } else if (e.getNumberType().equals(2)) {
                multiPerson.add(_levelJumpDTO);
            }
        }
        Map map = new HashMap();
        map.put("singlePerson", singlePerson);
        map.put("multiPerson", multiPerson);

        return map;
    }


    @Override
    public LevelJump findById(Integer id) {
        return levelJumpRepository.findById(id).orElse(null);
    }


    @Override
    public List<LevelCondition> findAllLevelCondition() {

        return levelConditionRepository.findAll();
    }

    @Override
    public List<LevelHonor> findAllLevelHonor() {

        return levelHonorRepository.findAll();
    }
}
