package com.example.shalantor.connect4;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class MenuActivity extends SurfaceView implements Runnable{

    private volatile boolean showingMenu;                //to stop animations on menu if not necessary
    private Canvas canvas;
    private List<CustomRect> listOfFallingChips;      //list of the chips that will fall fro mtop to bottom

    /*Dimensions needed for menu screen*/
    private int screenWidth;
    private int screenHeight;
    private int chipDimension;

    private long lastFrameTime;                 //for fps

    /*Bitmaps with the images*/
    private Bitmap redChip;
    private Bitmap yellowChip;

    private Thread thread = null;
    private SurfaceHolder holder;
    private Paint paint;

    public MenuActivity(Context context){
        super(context);
        showingMenu = true;
        listOfFallingChips = new ArrayList<>();

        /*Type cast to get screen resolution*/
        Activity parent = (Activity) context;
        holder = getHolder();

        /*get screen dimensions*/
        Display display = parent.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
        chipDimension = screenWidth / 10;

        /*Load images*/
        redChip = BitmapFactory.decodeResource(getResources(),R.mipmap.redchip);
        yellowChip = BitmapFactory.decodeResource(getResources(),R.mipmap.yellowchip);

    }

    /*Simple run method*/
    @Override
    public void run(){
        while(showingMenu){
            updateElements();
            drawElements();
            controlFPS();
        }
    }

    /*Method to update position of elements in menu screen*/
    public void updateElements(){

        /*Remove chips which have fallen through whole screen*/
        for (Iterator<CustomRect> iterator = listOfFallingChips.iterator(); iterator.hasNext();) {
            CustomRect rect = iterator.next();
            if (rect.getRect().top >= screenHeight) {
                iterator.remove();
            }
        }

        if(listOfFallingChips.size() < 3) {     /*Don't add more than 3 chips at a time*/

            /*Now add a chip at random*/
            if (Math.random() > 0.5) {
                /*Coordinates*/
                Random generator = new Random();
                int left = generator.nextInt(screenWidth - chipDimension);
                int right = left + chipDimension;
                int bottom = 0;
                int top = -chipDimension;

                /*rect and colors*/
                Rect rect = new Rect(left, top, right, bottom);
                Bitmap chip;
                if (Math.random() > 0.5) {
                    chip = yellowChip;
                } else {
                    chip = redChip;
                }
                listOfFallingChips.add(new CustomRect(chip, rect));
            }
        }

        /*Now move all chips down a bit*/
        for(CustomRect customRect : listOfFallingChips){
            customRect.getRect().top += chipDimension / 10;
            customRect.getRect().bottom += chipDimension / 10;
        }


    }

    /*Method to render the elements*/
    public void drawElements(){

        if(holder.getSurface().isValid()){
            canvas = holder.lockCanvas();
            paint = new Paint();

            /*Make background blue*/
            canvas.drawColor(Color.BLUE);

            /*Draw the falling chips*/
            for(CustomRect customRect : listOfFallingChips){
                canvas.drawBitmap(customRect.getBitmap(),null,customRect.getRect(),paint);
            }

            holder.unlockCanvasAndPost(canvas);
        }

    }

    /*Method to control the refresh rate of screen*/
    public void controlFPS(){

        long timeThisFrame = System.currentTimeMillis() - lastFrameTime;
        long timeToSleep = 50 - timeThisFrame;

        if(timeToSleep > 0){
            try{
                thread.sleep(timeToSleep);
            }catch (InterruptedException ex){
                Log.d("THREAD_SLEEP","Interrupted exception occured");
            }
        }

        lastFrameTime = System.currentTimeMillis();

    }

    /*To pause animation*/
    public void pause(){
        showingMenu = false;
        try{
            thread.join();
        }catch (InterruptedException ex){
            Log.d("THREAD_JOIN","Interrupted exception occured");
        }
    }

    /*To continue animation from a pause*/
    public void resume(){
        showingMenu = true;
        thread = new Thread(this);
        thread.start();
    }

}

@@ -2628,17 +2628,42 @@
 ize() %3C 
-3
+4 && areChipsApartEnough()
 ) %7B     
@@ -2688,9 +2688,9 @@
 han 
-3
+4
  chi
@@ -2747,69 +2747,10 @@
 ndom
-*/%0A            if (Math.random() %3E 0.5) %7B%0A                /*C
+ c
 oord
@@ -2770,20 +2770,16 @@
         
-    
 Random g
@@ -2799,28 +2799,24 @@
 w Random();%0A
-    
             
@@ -2882,28 +2882,24 @@
             
-  
-  
 int right = 
@@ -2928,28 +2928,24 @@
             
-    
 int bottom =
@@ -2948,20 +2948,16 @@
 om = 0;%0A
-    
         
@@ -2999,20 +2999,16 @@
         
-    
 /*rect a
@@ -3015,28 +3015,24 @@
 nd colors*/%0A
-    
             
@@ -3091,20 +3091,16 @@
         
-  
-  
 Bitmap c
@@ -3100,28 +3100,24 @@
 itmap chip;%0A
-    
             
@@ -3139,28 +3139,24 @@
 () %3E 0.5) %7B%0A
-    
             
@@ -3190,20 +3190,16 @@
         
-    
 %7D else %7B
@@ -3199,20 +3199,16 @@
  else %7B%0A
-    
         
@@ -3239,34 +3239,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -3309,30 +3309,16 @@
 rect));%0A
-            %7D%0A
         
@@ -5008,10 +5008,320 @@
 %0A    %7D%0A%0A
+    /*Method to check if chips are apart enough from each other*/%0A    private boolean areChipsApartEnough()%7B%0A        for(CustomRect customRect : listOfFallingChips)%7B%0A            if(customRect.getRect().top %3C= screenHeight / 4)%7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A%0A
 %7D%0A

Add better algorithm for chip spawning