package cancergrowth;

import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.lang.Math;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Scanner;

/*
 * File Name: Cell.java
 *   Created: Nov 7, 2018
 *    Author: 
 */
public class Cell extends Object
{

  private int xval;
  private int yval;
  private ArrayList<Integer> stages = new ArrayList<Integer>();
  private int currentStage;
  private int currentTime;
  private static final BufferedImage p0;
  private static BufferedImage[] p1 = new BufferedImage[12];
  private static final BufferedImage p2;
  private static final BufferedImage p3;
  private static final BufferedImage p4;

  static
  {
    boolean transparent = true;
    for (int count = 0; count < p1.length; count++)
    {
      BufferedImage g1 = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
      Graphics gg1 = g1.getGraphics();
      if (transparent)
      {
        gg1.setColor(new Color(255, 0, 0, 127));
      }
      else
      {
        gg1.setColor(new Color(255, 0, 0));
      }
      int width;
      int pos;
      width = (int) ((count + 1) * 2 + 22.5);
      pos = (50 - width) / 2;
      gg1.fillOval(pos, pos, width, width);
      gg1.setColor(Color.BLACK);
      gg1.drawOval(pos, pos, width, width);
      gg1.drawString("G1", 18, 30);
      p1[count] = g1;
    }

    BufferedImage s = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
    Graphics gs = s.getGraphics();
    if (transparent)
    {
      gs.setColor(new Color(255, 255, 0, 127));
    }
    else
    {
      gs.setColor(new Color(255, 255, 0));
    }
    gs.fillOval(0, 0, 50, 50);
    gs.setColor(Color.BLACK);
    gs.drawOval(0, 0, 50, 50);
    gs.drawString("S", 22, 30);
    p2 = s;
    BufferedImage g2 = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
    Graphics gg2 = g2.getGraphics();
    if (transparent)
    {
      gg2.setColor(new Color(0, 255, 0, 127));
    }
    else
    {
      gg2.setColor(new Color(0, 255, 0));
    }
    gg2.fillOval(0, 0, 50, 50);
    gg2.setColor(Color.BLACK);
    gg2.drawOval(0, 0, 50, 50);
    gg2.drawString("G2", 18, 30);
    p3 = g2;
    BufferedImage m = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
    Graphics gm = m.getGraphics();
    if (transparent)
    {
      gm.setColor(new Color(0, 0, 255, 127));
    }
    else
    {
      gm.setColor(new Color(0, 0, 255));
    }
    gm.fillOval(0, 0, 50, 50);
    gm.setColor(Color.BLACK);
    gm.drawOval(0, 0, 50, 50);
    gm.drawString("M", 22, 30);
    p4 = m;
    BufferedImage g0 = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
    Graphics gg0 = g0.getGraphics();
    if (transparent)
    {
      gg0.setColor(new Color(80, 80, 80, 127));
    }
    else
    {
      gm.setColor(new Color(80, 80, 80));
    }
    gg0.fillOval(0, 0, 50, 50);
    gg0.setColor(Color.BLACK);
    gg0.drawOval(0, 0, 50, 50);
    gg0.drawString("G0", 18, 30);
    p0 = g0;
  }

  public Cell(int x, int y)
  {
    xval = x;
    yval = y;
    stages.add(10 + (int) (Math.random() * 3 - 1));
    stages.add(6 + (int) (Math.random() * 3 - 1));
    stages.add(3 + (int) (Math.random() * 3 - 1));
    stages.add(1 + (int) (Math.random() * 2));
    currentTime = 0;
    currentStage = 0;
  }

  public int getX()
  {
    return xval;
  }

  public int getY()
  {
    return yval;
  }

  /*public Color getColor()
  {
    return myColor;
  }*/
  public Cell performTime(boolean run)
  {
    if (!run || currentStage == 4)
    {
      if (run)
      {
        currentStage = 0;
      }
      else
      {
        int random = (int) (Math.random() * 10);
        if (random == 0)
        {
          currentStage = 4;
        }
        return this;
      }
    }
    this.currentTime++;
    Cell newcell = this;
    //System.out.println("Increased Current Time to:" + this.currentTime);
    if (this.currentTime > stages.get(this.currentStage))
    {
      if (currentStage == 3)
      {
        // SPLIT
        int width = (int) (-50 + Math.random() * 101);
        int height = (int) (-50 + Math.random() * 101);
        while (width < 20 && width > -20)
        {
          width = (int) (-50 + Math.random() * 101);
        }
        while (height < 20 && height > -20)
        {
          height = (int) (-50 + Math.random() * 101);
        }
        newcell = new Cell(xval + width, yval + height);
        //stages = new ArrayList<Integer>();
        //stages.add(10 + (int) Math.random() * 3 - 1);
        //stages.add(7 + (int) Math.random() * 3 - 1);
        //stages.add(3 + (int) Math.random() * 3 - 1);
        //stages.add(1 + (int) Math.random() * 2);

        xval -= width;
        yval -= height;
        currentStage = 0;
        currentTime = 1;
      }
      else
      {
        this.currentStage++;
        this.currentTime = 0;
        //if(currentStage == 1) myColor = new Color(255, 255, 0, 100);
        //if(currentStage == 2) myColor = new Color(0, 255, 0, 100);
        //if(currentStage == 3) myColor = new Color(0, 0, 255, 100);
      }
    }
    return newcell;
  }

  public Image getImage()
  {
    if (currentStage == 0)
    {
      return p1[currentTime];
    }
    if (currentStage == 1)
    {
      return p2;
    }
    if (currentStage == 2)
    {
      return p3;
    }
    if (currentStage == 3)
    {
      return p4;
    }
    return p0;
  }

}
