package lotto;

/**
* LottoBoard.java defines LottoBoard class
*/
import javax.swing.*;
import javax.swing.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.geom.*;

public class LottoBoard extends JPanel {
    int nrows, ncolumns;
    GridLayout glay ;
    LottoSquare[] lottoSquare;
    int N;   // Number of items
    LottoModel lottoModel;
    Font f = new Font("Serif",Font.BOLD,24);
    private Color dkGreen = new Color(0,140,0); 
    private Color dkBlue = new Color(0,0,140);
    Color[] colors = {dkBlue, Color.magenta, Color.red, Color.orange};

    /**
    * constructor for LottoBoard
    *
    * @param lm  the LottoModel with the data
    */
    public LottoBoard(LottoModel lm) {
        lottoModel = lm;
        setBackground(dkGreen);
        updateLottoBoard(lm);
        }

    /**
    *update the  LottoBoard to the current data
    *
    * @param lm  the LottoModel with the data
    */
    public void updateLottoBoard ( LottoModel lm) {
        lottoModel = lm;
        int prod;
        N = lm.getN();
        removeAll();
        this.invalidate();
        
        nrows=nrowsCalc(N);
        ncolumns=ncolumnsCalc(N, nrows);
        prod = nrows*ncolumns;
        glay = new GridLayout(nrows, ncolumns, 8, 8);
        setLayout(glay);
        lottoSquare = new LottoSquare[prod];
        for (int i=0; i< N; i++) {
            int mode = lottoModel.getMode(i);
            //int mode = 0;
            lottoSquare[i] = new LottoSquare(String.valueOf(i+1),mode);
            //lottoSquare[i].setBorder(new BevelBorder(BevelBorder.RAISED));
            //lottoSquare[i].setHorizontalAlignment(SwingConstants.CENTER);
            //lottoSquare[i].setOpaque(true);
            //lottoSquare[i].setFont(f);
            //lottoSquare[i].setBackground(colors[mode]);
            //lottoSquare[i].setForeground(Color.white);
            IndexedMouseListener  iml = new IndexedMouseListener(i);
            lottoSquare[i].addMouseListener(iml);
            add(lottoSquare[i]);
        }
       
                
        this.validate();
        }

    private  int nrowsCalc(int N) {
        int nrows;
        if (N< 6) { 
            nrows =1;
            }
        else {
            nrows = (int)Math.floor(Math.sqrt(N));
            }
        return nrows;
        }

    private int ncolumnsCalc( int N, int nrows) {
        int ncolumns;
        if (nrows == 1) {
            ncolumns =N;
            }
        else {
            if (N%nrows == 0) {
                ncolumns = N / nrows;
                }
            else {
                ncolumns = N/(nrows -1);
                }		

            }
        return ncolumns;
        }
        
            

    public Dimension getPreferredSize(){
        Dimension d = new Dimension(350, 250);
        return d;
        }
    


    
    private class IndexedMouseListener implements MouseListener{
        int index;
        public IndexedMouseListener(int i){
        index =i;
    }
        
        public void mousePressed(MouseEvent e){
            lottoModel.toggleChosen(index);
            int mode = lottoModel.getMode(index);
            lottoSquare[index].setMode(mode);
            lottoSquare[index].repaint();
        }
        //public void mouseDragged(MouseEvent e){;}
        public void mouseReleased(MouseEvent e){;}
        public void mouseClicked(MouseEvent e){;}
        public void mouseEntered(MouseEvent e){;}
        public void mouseExited(MouseEvent e){;}
    }


}
        
