package com.td.boss.game.complayergoods.controller;

import com.td.boss.common.pojo.Result;
import com.td.boss.game.commallseed.service.ComMallSeedService;
import com.td.boss.game.commallseed.vo.ComMallSeedVo;
import com.td.boss.game.complayerland.pojo.ComPlayerLand;
import com.td.boss.game.complayerland.service.ComPlayerLandService;
import com.td.boss.game.complayerprofit.pojo.ComPlayerProfit;
import com.td.boss.game.complayerprofit.service.ComPlayerProfitService;
import com.td.boss.game.complayerprofit.vo.ComPlayerProfitVo;
import com.td.boss.game.complayersattri.service.ComPlayersAttriService;
import com.td.boss.game.complayersattri.vo.ComPlayersAttriVo;
import com.td.boss.game.comusers.pojo.ComUsers;
import com.td.boss.game.comusers.service.ComUsersService;
import com.td.boss.game.comusers.vo.ComUsersVo;
import com.td.boss.util.CopyUtil;
import com.td.boss.util.DoubleUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ComPlayerGoodsControllerTest {
    private int threadCount = 5; // 子线程
    private CountDownLatch countDownLatch = new CountDownLatch(threadCount);

    private String otherUserId = "4";
    private Integer otherLandId = 5;

    @Autowired
    private ComPlayerGoodsController comPlayerGoodsController;

    @Autowired
    private ComUsersService comUsersService;

    @Autowired
    private ComPlayersAttriService comPlayersAttriService;

    @Autowired
    private ComPlayerProfitService comPlayerProfitService;

    @Autowired
    private ComPlayerLandService comPlayerLandService;

    @Autowired
    private ComMallSeedService comMallSeedService;

    @Test
    public void test() {
        //全部玩家，进行偷取测试
        ComPlayersAttriVo comPlayersAttriVo = new ComPlayersAttriVo();
        comPlayersAttriVo.setSidx("desc");
        comPlayersAttriVo.setSord("createTime");
        List<ComPlayersAttriVo> comPlayersAttriVoList = comPlayersAttriService.list(comPlayersAttriVo).getData();
        List<String> userIdList = new ArrayList<>();
        for (int a = 0; a < threadCount; a++) {
            //for(int j = 0;j<10;j++){
            //    userIdList.add(comPlayersAttriVoList.get(a*5 + j ).getUserId());
            //}
            Collections.shuffle(comPlayersAttriVoList);
            userIdList = comPlayersAttriVoList.stream().map(e -> e.getUserId()).collect(Collectors.toList());
            MyThread myThread = new MyThread(countDownLatch, userIdList);
            Thread thread = new Thread(myThread);
            thread.start();
            try {
                Thread.sleep(1L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            countDownLatch.await();
        } catch (Exception e) {

        }
    }

    class MyThread implements Runnable {
        private CountDownLatch countDownLatch;
        List<String> searchVoList;

        public MyThread(CountDownLatch countDownLatch, List<String> searchVoList) {
            this.countDownLatch = countDownLatch;
            this.searchVoList = searchVoList;
        }

        @Override
        public void run() {

            try {
                for (String userIds : this.searchVoList) {
                    if (userIds.equals(otherUserId)) {

                        //测试受灾情况
                        //disasterFunction();
                        continue;
                    }

                    Thread.sleep(1L);
                    Result<Map> result = comPlayerGoodsController.stealFruitFunction(userIds, otherUserId, otherLandId);
                    if(result.getCode().equals(713)){

                        //目标结束后收取果实
                        comPlayerGoodsController.addFruitFunction(otherUserId, otherLandId);
                        return;
                    }
                }

                countDownLatch.await();
            } catch (Exception e) {

            }
            //执行业务代码
            countDownLatch.countDown();
        }
    }

    @Test
    void addFruitFunction() {
        comPlayerGoodsController.addFruitFunction(otherUserId, otherLandId);
    }

    @Test
    void disasterFunction() {

        ComPlayerLand _comPlayerLand = comPlayerLandService.findByUserIdAndLandId(otherUserId,otherLandId);
        ComMallSeedVo comMallSeedVo =  comMallSeedService.findById(_comPlayerLand.getPlantId());
        List<ComPlayerProfit> comPlayerProfits = comPlayerProfitService.findByUserIdAndPlantFlagAndLandId(_comPlayerLand.getUserId(), _comPlayerLand.getPlantFlag(), _comPlayerLand.getConfigLandId());
        //todo 总共受损的数量
        Double _allLossProfit = 0d;
        boolean isNaturalProfit = false, isBeastProfit = false;
        Integer _disasterCount = 0;
        for (int j = 0; j < comPlayerProfits.size(); j++) {
            ComPlayerProfit _profit = comPlayerProfits.get(j);
            _allLossProfit = DoubleUtil.add(_allLossProfit, _profit.getStolen());
            if (_profit.getLossType().equals(2)) {
                if(DoubleUtil.compare(-1D, _profit.getDsasterId().doubleValue()).equals(0)){
                    isNaturalProfit = true;
                }
                //出现灾难的次数
                _disasterCount++;
            } else if (_profit.getLossType().equals(3)) {
                if(DoubleUtil.compare(-1D, _profit.getDsasterId().doubleValue()).equals(0)){
                    isBeastProfit = true;
                }
                //出现灾难的次数
                _disasterCount++;
            }
        }


        try {
            ComPlayerProfitVo naturalProfitVo = comPlayerProfitService.addFirstDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
                    comMallSeedVo, 2, _allLossProfit, -1);
            //多次执行需要加上当前操作的利润
            _allLossProfit += naturalProfitVo.getStolen();
            _disasterCount ++;

            ComPlayerProfitVo beastProfitVo = comPlayerProfitService.addFirstDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
                    comMallSeedVo, 3, _allLossProfit, -1);
            //多次执行需要加上当前操作的利润
            _allLossProfit += beastProfitVo.getStolen();
            _disasterCount ++;

            //测试用
            ComPlayerProfitVo _profitVo1 = comPlayerProfitService.addOtherDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
                    comMallSeedVo, 2, _allLossProfit, _disasterCount, -2);
            _allLossProfit += _profitVo1.getStolen();
            _disasterCount ++;

            Thread.sleep(10L);

            ComPlayerProfitVo _profitVo2 = comPlayerProfitService.addOtherDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
                    comMallSeedVo, 2, _allLossProfit, _disasterCount, -2);
            _allLossProfit += _profitVo2.getStolen();
            _disasterCount ++;

            Thread.sleep(10L);

            ComPlayerProfitVo _profitVo3 = comPlayerProfitService.addOtherDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
                    comMallSeedVo, 2, _allLossProfit, _disasterCount, -2);
            _allLossProfit += _profitVo3.getStolen();
            _disasterCount ++;
        }catch (Exception e){

        }

    }

    @Test
    void stealFruitFunction() {
    }

    @Test
    void saleFruitFunction() {
    }

    @Test
    void grantFruitFunction() {
    }
}