/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gamegraphics;

import frame.Player;
import gamegraphics.elements.CollectorHouse;
import gamegraphics.elements.House;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Ani
 */
public class Gametable extends JPanel{
    private Image table;
    private House houses[][];
    private CollectorHouse[] collectorHouses;
    private int row;
    private int house;
    private Container container;
    private JTextField noFiguresInHouses[];
    private JTextField noFiguresInCollectors[];
    private Player playerHuman;


    public Gametable(Image table, House[][] houses, CollectorHouse[] collectorHouse,
            JTextField noFiguresInHouses[], JTextField noFiguresInCollectors[],
            Player playerHuman, Container container) {
        super(null, true);
        this.table = table;
        this.houses = houses;
        this.collectorHouses = collectorHouse;
        this.noFiguresInHouses = noFiguresInHouses;
        this.noFiguresInCollectors = noFiguresInCollectors;
        this.container = container;
        this.playerHuman = playerHuman;
    }

    @Override
    public void paint(Graphics g) {
        update(g);
    }

    @Override
    public void update(Graphics g) {
        g.drawImage(table, 0, 0, null);
        paintHouses(g);
    }

    private void paintHouses(Graphics g){
        for (int j = 1; j < 3; j++){
            for (int i = 1; i < 7; i++){
                houses[j][i].paint(g);
            }
            collectorHouses[j].paint(g);
        }
    }

    public void sowAndCollectAnimation(Player player, int row, int house){
        try {
            this.row = row;
            this.house = house;
            sow(row, house);
            collect(player);
        } catch (InterruptedException ex) {

        }
    }

    private void sow(int sowRow, int sowHouse) throws InterruptedException{
        int noFigures = houses[sowRow][sowHouse].getNumberOfFigures();
        houses[sowRow][sowHouse].removeFigures();
        row = sowRow;
        house = sowHouse;
        if (sowRow==playerHuman.getPlayerAsInteger()){
            noFiguresInHouses[sowHouse].setText("  " + houses[playerHuman.getPlayerAsInteger()][sowHouse].getNumberOfFigures());
        }
        container.repaint();
        Thread.sleep(500);

        while(noFigures>0){

            if (noFigures>0){
                sowRow(sowRow, sowHouse+1, Math.min(6, noFigures+sowHouse));
                row = sowRow;
                house = Math.min(6, noFigures+sowHouse);
                noFigures = noFigures - 6 + sowHouse;
            }
            if (noFigures>0){
                sowRow(sowRow==1 ? 2 : 1, 1, Math.min(6, noFigures));
                row = sowRow==1 ? 2 : 1;
                house =  Math.min(6, noFigures);
                noFigures = noFigures - Math.min(6, noFigures);
            }
            if (noFigures>0){
                sowRow(sowRow, 1, Math.min(sowHouse, noFigures));
                row = sowRow;
                house = Math.min(sowHouse, noFigures);
                noFigures = noFigures - Math.min(sowHouse, noFigures);               
            }
        }

    }

    private void sowRow(int row, int from, int to) throws InterruptedException{
        for (int i = from; i<=to; i++){
            houses[row][i].addFigure();
            if (row==playerHuman.getPlayerAsInteger()){
                noFiguresInHouses[i].setText("  "+houses[row][i].getNumberOfFigures());
            }
            container.repaint();
            Thread.sleep(500);
        }
    }

    private void collect(Player player) throws InterruptedException{
        if (houses[row][house].getNumberOfFigures()==2 || houses[row][house].getNumberOfFigures()==4){
            if (collectRow(player.getPlayerAsInteger(), row, house, 1)){
                if (collectRow(player.getPlayerAsInteger(), row==1 ? 2 : 1, 6, 1)){
                    collectRow(player.getPlayerAsInteger(), row, 6, house+1);
                }
            }
        }
    }

    private boolean collectRow(int who, int crow, int from, int to) throws InterruptedException{
        for (int i = from; i>=to; i--){
            if (houses[crow][i].getNumberOfFigures()==2 || houses[crow][i].getNumberOfFigures()==4){
                int noFigures = houses[crow][i].getNumberOfFigures();
                houses[crow][i].removeFigures();
                if (row==playerHuman.getPlayerAsInteger()){
                    noFiguresInHouses[i].setText("  "+houses[row][i].getNumberOfFigures());
                }
                container.repaint();
                Thread.sleep(500);
                collectorHouses[who].addFigures(noFigures);
                noFiguresInCollectors[who].setText("     "+collectorHouses[who].getNumberOfFigures());
                container.repaint();
                Thread.sleep(500);
            }
            else{
                return false;
            }
        }
        return true;
    }










}
