/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sudoku.main.view;

import com.sudoku.grid.gridcells.IhmGridLinesCompleted;
import com.sudoku.grid.player.IhmGridPlayer;
import com.sudoku.grid.popups.IhmPopupsList;
import com.sudoku.main.manager.RefreshGridPlayer;
import com.sudoku.data.manager.GridManager;
import com.sudoku.data.model.Grid;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.stage.WindowEvent;

/**
 * FXML Controller class
 *
 * @author MOURAD
 */
public class GridPlayerGameController implements Initializable, ControlledScreen,EventHandler<IhmGridLinesCompleted>{
    
    ScreensController myController;
    IhmGridPlayer GridP;
    
    @FXML
    private ScrollPane GridPlayerContainer;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }
    
    @Override
    public void setScreenParents(ScreensController screenParent) {
        myController = screenParent;
        final GridPlayerGameController gridPlayerGameController = this;
        myController.addEventHandler(WindowEvent.WINDOW_SHOWING ,new EventHandler<WindowEvent>() {
                @Override public void handle(WindowEvent e) {
                    RefreshGridPlayer instance = RefreshGridPlayer.getInstance();
                    if(instance.getCurrentGrid() != null)
                    {
                          IhmPopupsList.getInstance().killAllTimers();
                          GridP = new IhmGridPlayer(instance.getCurrentGrid());
                          GridPlayerContainer.setContent(GridP);
                          GridP.addEventHandler(IhmGridLinesCompleted.GRID_COMPLETED, gridPlayerGameController);
                    }
                }
          });
    }
    
    @FXML
    private void goToProgram(ActionEvent event) {
        Grid currentGrid = RefreshGridPlayer.getInstance().getCurrentGrid();
        if(currentGrid != null)
            GridManager.getInstance().addPlayedGrid(currentGrid,currentGrid.getCreateUser());
        myController.setScreen(SudukoIHM.programID);
    }

    @Override
    public void handle(IhmGridLinesCompleted t) {
        if(t.isConsumed())
        {
            return ;
        }

        if(t.getEventType()==IhmGridLinesCompleted.GRID_COMPLETED)
        {
            t.consume();
            Grid currentGrid = RefreshGridPlayer.getInstance().getCurrentGrid();
            //currentGrid.setGrid(t.grid.get);
            GridManager.getInstance().addPlayedGrid(currentGrid,currentGrid.getCreateUser());
            myController.setScreen(SudukoIHM.programID);
        }
    }
}

@@ -495,20 +495,153 @@
 ort 
-java.net.URL
+com.sudoku.data.model.Cell;%0Aimport com.sudoku.grid.gridcells.IhmCell;%0Aimport java.math.BigInteger;%0Aimport java.net.URL;%0Aimport java.sql.Timestamp
 ;%0Aim
@@ -2613,24 +2613,68 @@
 pleted t) %7B%0A
+        IhmGridLinesCompleted instance = t;%0A
         if(t
@@ -2929,40 +2929,57 @@
     
-//currentGrid.setGrid(t.grid.get
+Grid finishedGrid = copyGrid(currentGrid,instance
 );%0A 
@@ -3029,35 +3029,37 @@
 yedGrid(
-currentGrid,current
+finishedGrid,finished
 Grid.get
@@ -3146,10 +3146,1411 @@
 %7D%0A    %7D%0A
+    %0A    public Cell%5B%5D%5B%5D updateCells(IhmGridLinesCompleted t)%0A    %7B%0A        Cell%5B%5D%5B%5D UpdatedCells=new Cell%5B9%5D%5B9%5D;%0A        IhmCell%5B%5D%5B%5D currentState=t.grid.getCells();%0A        int X,Y,value;%0A        byte xByte,yByte,valueByte;%0A        int i2,y2;%0A        for(int i=0;i%3C9;i++)%0A        %7B%0A            for(int j=0;j%3C9;j++)%0A            %7B%0A                UpdatedCells%5Bi%5D%5Bj%5D = new Cell();%0A                X=currentState%5Bi%5D%5Bj%5D.getX();%0A                Y=currentState%5Bi%5D%5Bj%5D.getY();%0A                value = currentState%5Bi%5D%5Bj%5D.getValue();%0A                xByte = (byte) X;%0A                yByte = (byte) Y;%0A                valueByte= (byte) value;%0A                UpdatedCells%5Bi%5D%5Bj%5D.setX( xByte);%0A                UpdatedCells%5Bi%5D%5Bj%5D.setY( yByte);%0A                UpdatedCells%5Bi%5D%5Bj%5D.setValue(valueByte);%0A                /*i2 = UpdatedCells%5Bi%5D%5Bj%5D.getValue() & 0xFF;%0A                System.out.println(valueByte);*/%0A            %7D%0A        %7D%0A        return UpdatedCells;%0A    %7D%0A    %0A    public Grid copyGrid(Grid g,IhmGridLinesCompleted completed)%0A    %7B%0A        Grid newInstance = new Grid(g.getTitle(), g.getCreateUser());%0A        newInstance.setComments(g.getComments());%0A        newInstance.setCreateDate((Timestamp) g.getCreateDate());%0A        newInstance.setDifficulty(g.getDifficulty());%0A        newInstance.setGrid(updateCells(completed));%0A        newInstance.setTags(g.getTags());%0A        return newInstance;%0A    %7D%0A
 %7D%0A

create new played grid after finish playing.