package ichttt.battleship.logic;

import ichttt.battleship.Battleship;
import ichttt.battleship.gui.GuiBattleShip;

import javax.swing.*;

/**
 * Created by Tobias on 16.11.2016.
 */
public class WinningCondition {
    public static int foundp1 = 0;
    public static int foundp2 = 0;
    public static int max;
    public static boolean checkShipDown = true;

    public static void checkWin(int posx, int posy, boolean isP1) {
        if(checkShipDown)
    	    checkShipDown(posx, posy, isP1);
        if (foundp1 == max&&isP1) {
            JOptionPane.showMessageDialog(null, "Player 1 wins!");
            GuiBattleShip.loadFromBoolean(Battleship.player1);
        }
        else if(foundp2==max&&!isP1) {
        	JOptionPane.showMessageDialog(null, "Player 2 hat wins!");
            GuiBattleShip.loadFromBoolean(Battleship.player2);
        }

    }
    
    
    private static int getPossibleShip(HitTable shipRows[], int curposx, int curposy) {
        for(int i1 = 0;i1<shipRows.length;i1++) {
            for(int i2 = 0;i2<shipRows[0].size();i2++) {
                if(shipRows[i1].posy[i2]==curposy&&shipRows[i1].posx[i2]==curposx) {
                	System.out.println("Found possible Ship at " + i1);
                    return i1;
                }
            }
        }
        return -1;
    }
    
    
	public static void checkShipDown(int curposx, int curposy, boolean isP1) {
        //TODO something isn't working here
        boolean isDestroyed = true;
        int possibleShip;
        if(!isP1) {
        	possibleShip = getPossibleShip(Battleship.shipRowsP1, curposx, curposy);
	        if(possibleShip !=-1) {
	            for(int i = 0; i< Battleship.shipRowsP1[possibleShip].size(); i++) {
	            	if(Battleship.shipRowsP1[possibleShip].posx[i]==-1&& Battleship.shipRowsP1[possibleShip].posy[i]==-1)
	            		break;
	            	if(Battleship.player2hit[Battleship.shipRowsP1[possibleShip].posy[i]][Battleship.shipRowsP1[possibleShip].posx[i]]==null||
	            		Battleship.player2hit[Battleship.shipRowsP1[possibleShip].posy[i]][Battleship.shipRowsP1[possibleShip].posx[i]].equals("O")) {
						isDestroyed = false;
						System.out.println("Ship candidate invalid because of " + Battleship.shipRowsP1[possibleShip].posx[i] +" Y "+ Battleship.shipRowsP1[possibleShip].posy[i]);
						break;
	            	}
	            }
	        }
	        else {
                isDestroyed = false;
            }
        }
        else {
        	possibleShip = getPossibleShip(Battleship.shipRowsP2, curposx, curposy);
	        if(possibleShip !=-1) {
	            for(int i = 0; i< Battleship.shipRowsP2[possibleShip].size(); i++) {
	            	if(Battleship.shipRowsP2[possibleShip].posx[i]==-1&& Battleship.shipRowsP2[possibleShip].posy[i]==-1)
	            		break;
	            	if(Battleship.player1hit[Battleship.shipRowsP2[possibleShip].posy[i]][Battleship.shipRowsP2[possibleShip].posx[i]]==null||
	            		Battleship.player1hit[Battleship.shipRowsP2[possibleShip].posy[i]][Battleship.shipRowsP2[possibleShip].posx[i]].equals("O")) {
						isDestroyed = false;
						System.out.println("Ship candiate invalid because of " + Battleship.shipRowsP2[possibleShip].posx[i] +" Y "+ Battleship.shipRowsP2[possibleShip].posy[i]);
						break;
	            	}
	            }
	        }
	        else {
                isDestroyed = false;
            }
        }
        if(isDestroyed) {
        	JOptionPane.showMessageDialog(null, "Ship down!");
        }
	}
}

@@ -393,17 +393,17 @@
 (int pos
-x
+y
 , int po
@@ -403,17 +403,17 @@
  int pos
-y
+x
 , boolea
@@ -478,15 +478,15 @@
 (pos
-x
+y
 , pos
-y
+x
 , is
@@ -925,33 +925,33 @@
 ws%5B%5D, int curpos
-x
+y
 , int curposy) %7B
@@ -946,17 +946,17 @@
 t curpos
-y
+x
 ) %7B%0A    
@@ -1364,17 +1364,17 @@
 t curpos
-x
+y
 , int cu
@@ -1377,17 +1377,17 @@
 t curpos
-y
+x
 , boolea
@@ -1400,52 +1400,8 @@
 ) %7B%0A
-        //TODO something isn't working here%0A
     
@@ -1543,33 +1543,33 @@
 wsP1, curpos
-x
+y
 , curpos
 y);%0A%09       
@@ -1548,33 +1548,33 @@
  curposy, curpos
-y
+x
 );%0A%09        if(p
@@ -2501,17 +2501,17 @@
 rpos
-x
+y
 , curpos
 y);%0A
@@ -2506,17 +2506,17 @@
 , curpos
-y
+x
 );%0A%09    

Fix showShipDown