package com.yaohun.wheatwar.game;

import com.yaohun.wheatwar.WheatWar;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import tools.BossBarUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Game {

    private World world;
    private Region region;
    private BossBar bossBar1;
    private BossBar bossBar2;
    private boolean started = false;
    private Location centralPoint;
    private List<Location> wheatList = new ArrayList<>();
    private List<BukkitTask> tasks = new ArrayList<>();
    public Game(){
        this.world = Bukkit.getWorld("world");
        this.region = new Region(world,new Point(-30,68,-33),new Point(32,68,29));
        this.centralPoint = new Location(world,1,72,-2);
        this.bossBar1 = Bukkit.createBossBar("今日挑战进度", BarColor.WHITE, BarStyle.SEGMENTED_10);
        this.bossBar2 = Bukkit.createBossBar("补种进度", BarColor.GREEN, BarStyle.SEGMENTED_20);
        loadGameMapFarmland();
    }

    private void updateCompleteWheatGoalBossBar() {
        int cNow = GameSite.getMatureWheatAmount();
        int cMax = wheatList.size();
        double d = (double) cNow / (double) cMax;
        String bossTitle = "§6成熟小麦数量§c: §f"+cNow+" §b("+String.format("%.2f",(d*100))+"%)";
        bossBar2.setTitle(bossTitle);
        BossBarUtil.setBarColor(bossBar2,d);
        BossBarUtil.setBarProgress(bossBar2,d);
    }



    public void start(){
        if (started) {
            return;
        }
        this.started = true;
        tasks.add(new BukkitRunnable() {
            @Override
            public void run() {
                updateCompleteWheatGoalBossBar();
            }
        }.runTaskTimer(WheatWar.inst(), 0L, 5L));
    }

    public void initPlayerData(Player player) {
        player.setArrowCooldown(6000);
        player.setGameMode(GameMode.SURVIVAL);
        player.setAllowFlight(true);
        // bossBar1.addPlayer(player);
        bossBar2.addPlayer(player);
    }


    public Location getFarmlandRandom(){
        List<Location> locationList = getWheatList();
        Collections.shuffle(locationList);
        Random random = new Random();
        return locationList.get(random.nextInt(locationList.size()));
    }
    // 获取一个未种植小麦的坐标
    public Location getAirWheatLocation(){
        Location location = null;
        List<Location> locationList = getWheatList();
        Collections.shuffle(locationList);
        for (Location loc : locationList){
            Block block = world.getBlockAt(loc);
            if (block.getType().equals(Material.AIR)) {
                location = loc;
                break;
            }
        }
        return location;
    }
    // 获取未成熟小麦的坐标
    public Location getAgeableWheatLocation(){
        Location location = null;
        List<Location> locationList = getWheatList();
        Collections.shuffle(locationList);
        for (Location loc : locationList){
            Block block = world.getBlockAt(loc);
            if (!block.getType().equals(Material.AIR) && block.getType().equals(Material.WHEAT)) {
                BlockData blockData = block.getBlockData();
                if (blockData instanceof Ageable ageable) {
                    if (ageable.getAge() < 7) {
                        location = loc;
                        break;
                    }
                }
            }
        }
        return location;
    }

    // 获取已成熟小麦的坐标
    public Location getMatureWheatLocation(){
        Location location = null;
        List<Location> locationList = getWheatList();
        Collections.shuffle(locationList);
        for (Location loc : locationList){
            Block block = world.getBlockAt(loc);
            if (!block.getType().equals(Material.AIR) && block.getType().equals(Material.WHEAT)) {
                BlockData blockData = block.getBlockData();
                if (blockData instanceof Ageable ageable) {
                    if (ageable.getAge() == 7) {
                        location = loc;
                        break;
                    }
                }
            }
        }
        return location;
    }
    // 优先寻找未种小麦的地    已种小麦未成熟的地
    public Location getPriorityRepairLocation(){
        Location location = null;
        List<Location> locationList = getWheatList();
        Collections.shuffle(locationList);
        for (Location loc : locationList){
            Block block = world.getBlockAt(loc);
            if(!block.getType().equals(Material.AIR) && block.getType().equals(Material.WHEAT)){
                BlockData blockData = block.getBlockData();
                if (blockData instanceof Ageable ageable) {
                    if (ageable.getAge() < 7) {
                        location = loc.clone().add(0.5,0,0.5);
                        break;
                    }
                }
            }else if (block.getType().equals(Material.AIR)) {
                location = loc.clone().add(0.5,0,0.5);
                break;
            }
        }
        return location;
    }


    // 优先寻找未种小麦的地    已种小麦未成熟的地
    public Location getPriorityClearLocation(){
        Location location = null;
        List<Location> locationList = getWheatList();
        Collections.shuffle(locationList);
        for (Location loc : locationList){
            Block block = world.getBlockAt(loc);
            if(block.getType().equals(Material.WHEAT)){
                BlockData blockData = block.getBlockData();
                if (blockData instanceof Ageable ageable) {
                    if(ageable.getAge() >= 7){
                        location = loc.clone().add(0.5,0,0.5);
                        break;
                    }else if (ageable.getAge() < 5) {
                        location = loc.clone().add(0.5,0,0.5);
                        break;
                    }
                }
            }
        }
        return location;
    }

    public World getWorld() {return world;}
    public List<Location> getWheatList() {return wheatList;}
    public Region getRegion() {return region;}
    public Location getCentralPoint() {return centralPoint;}
    public boolean isStarted() {return started;}
    public void addTasks(BukkitTask task){
        this.tasks.add(task);
    }

    public void loadGameMapFarmland(){
        List<Location> locationList = new ArrayList<>();
        for (int y = (int) Math.floor(67); y <= 67; y++) {
            for (int x = (int) Math.floor(region.getMin().getX()); x <= region.getMax().getX(); x++) {
                for (int z = (int) Math.floor(region.getMin().getZ()); z <= region.getMax().getZ(); z++) {
                    Block block = region.getWorld().getBlockAt(x, y, z);
                    if (!block.getType().equals(Material.AIR)) {
                        if(block.getType().equals(Material.FARMLAND)){
                            locationList.add(block.getLocation().clone().add(0,1,0));
                        }
                    }
                }
            }
        }
        this.wheatList = locationList;
        Bukkit.getConsoleSender().sendMessage("[日志 - 保卫小麦] 游戏地图:");
        Bukkit.getConsoleSender().sendMessage(" - "+this.wheatList.size()+"块小麦");
    }
}
