package com.td.Dawa.game.exceldrop.service;

import com.td.Dawa.common.service.*;
import com.td.Dawa.config.enums.GameEnum;
import com.td.Dawa.game.exceldrop.pojo.ExcelDrop;
import com.td.Dawa.game.exceldrop.vo.ExcelDropVo;
import com.td.Dawa.game.exceldrop.repository.ExcelDropRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ExcelDropServiceImpl extends CommonServiceImpl<ExcelDropVo, ExcelDrop, Integer> implements ExcelDropService{

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ExcelDropRepository excelDropRepository;

    @Override
    public List<ExcelDrop> getListBySceneId(Integer sceneId) {
        List<ExcelDrop> excelDrops = new ArrayList<>();
        List<Integer> ids = new ArrayList<>();
        if(sceneId.equals(GameEnum.MINE_01.getValue())){
            for(int i = 56;i<66;i++){
                ids.add(i);
            }
        }else if(sceneId.equals(GameEnum.MINE_02.getValue())){
            for(int i = 67;i<77;i++){
                ids.add(i);
            }
        }else if(sceneId.equals(GameEnum.MINE_03.getValue())){
            for(int i = 78;i<88;i++){
                ids.add(i);
            }
        }
        excelDrops =  excelDropRepository.findByIdIn(ids, Sort.by(Sort.Direction.ASC,"id"));
        return excelDrops;
    }

    @Override
    public List<ExcelDrop> getListByIds(Integer[] ids) {
        return  excelDropRepository.findByIdIn(ids, Sort.by(Sort.Direction.ASC,"id"));
    }

    @Override
    public List<ExcelDrop> getListByDropIds(Integer[] dropIds) {
        return  excelDropRepository.findByDropIdIn(dropIds, Sort.by(Sort.Direction.ASC,"dropId"));
    }

    @Override
    public ExcelDrop randomGetDropBySceneId(Integer sceneId) {
        List<Integer> ids = new ArrayList<>();
        if(sceneId.equals(GameEnum.MINE_01.getValue())){
            for(int i = 56;i<66;i++){
                ids.add(i);
            }
        }else if(sceneId.equals(GameEnum.MINE_02.getValue())){
            for(int i = 67;i<77;i++){
                ids.add(i);
            }
        }else if(sceneId.equals(GameEnum.MINE_03.getValue())){
            for(int i = 78;i<88;i++){
                ids.add(i);
            }
        }
        int index = (int) (Math.random()* ids.size());
        return excelDropRepository.findById(ids.get(index)).orElse(null);
    }
}
