package com.darkdimension.ritle_run;

import android.graphics.Canvas;
import android.graphics.Paint;

import java.util.Random;

public class Background
{
    private static DiPoint cloud1, cloud2, cloud3;
    public static DiAnimation aCloud = new DiAnimation(5);
    private static DiPoint[] mountain;
	private static int lastMountain = 0;

	private static Paint sky, sea;

    public static void Initialize()
    {
        //Clouds
        cloud1 = new DiPoint(3);
        cloud1.setCenterX(GameSurface.width/4);

        cloud2 = new DiPoint(3);
        cloud2.setCenterX(GameSurface.width *3/4);
        cloud2.setCenterY(GameSurface.height *3/4);

        cloud3 = new DiPoint(4);
        cloud3.setRight (GameSurface.width);
        cloud3.setBottom(GameSurface.height/2);

        aCloud.start();

        //Mountains
        mountain = new DiPoint[GameSurface.width /DiPoint.getWidth(1) +2];

        mountain[0] = new DiPoint(0);
        mountain[0].setBottom(GameSurface.height);
        mountain[0].setCenterX(0);

        mountain[1] = new DiPoint(2);
        mountain[1].setLeft(GameSurface.width -mountain[1].getWidth() *2/3);

        for (int iii = 2; iii < mountain.length; iii++)
        {
            //Image
            mountain[iii] = new DiPoint(0);//randomizer.nextInt(2));
            if (mountain[iii].getFrame() == mountain[iii-1].getFrame())
                mountain[iii].setFrame(2);

            //X Location
            mountain[iii].setLeft(mountain[iii-1].getRight());

            //Y Location
            if (mountain[iii].getFrame() != 2)
                mountain[iii].setBottom(GameSurface.height);
        }

		lastMountain = mountain.length -1;

		sky = new Paint();
		sky.setARGB(255, 254 ,158, 112);

		sea = new Paint();
		sea.setARGB(255, 88, 182, 222);
    }

    public static boolean animate(Random randomizer, int cameraX)
    {
		//Animate Clouds
		if (aCloud.update())
		if (aCloud.isAtEnd())
		{
			//Move Clouds
			cloud1.changeX(1);
			cloud2.changeX(1);
			cloud3.changeX(2);

			checkLimits(randomizer, cameraX);

			//Reset Animation
			aCloud.startFromBeginning();

			//Return updated
			return true;
		}
		return false;
    }

    public static void checkLimits(Random randomizer, int cameraX)
    {
		//Check Limits and relocate clouds
		if (cloud1.getRight() -cameraX/12 < 0)
			cloud1.setLeft(cameraX/12 +GameSurface.width);
		if (cloud1.getLeft() -cameraX/12 > GameSurface.width)
        	cloud1.setRight(0);

		if (cloud2.getRight() -cameraX/12 < 0)
			cloud2.setLeft(cameraX/12 +GameSurface.width);
		if (cloud2.getLeft() -cameraX/12 > GameSurface.width)
			cloud2.setRight(0);

        for (int iii = 0; iii < mountain.length; iii++)
            if (mountain[iii].getRight() -cameraX/9 < 0)
			{
				//Frame
				if (randomizer != null)
				{
					mountain[iii].setFrame(randomizer.nextInt(2));
					if (mountain[iii].getFrame() == mountain[lastMountain].getFrame())
						mountain[iii].setFrame(2);
				}

				//X coordinates
				if (mountain[lastMountain].getRight() -cameraX/9 > GameSurface.width)
					mountain[iii].setLeft(mountain[lastMountain].getRight());
				else
					mountain[iii].setLeft(cameraX / 9 + GameSurface.width);

				//Y coordinates
				if (mountain[iii].getFrame() == 2)
					mountain[iii].setTop(0);
				else
					mountain[iii].setBottom(GameSurface.height);

				lastMountain = iii;
			}

		if (cloud3.getRight() -cameraX/6 < 0)
			cloud3.setLeft(cameraX/6 +GameSurface.width);
		if (cloud3.getLeft() -cameraX/6 > GameSurface.width)
			cloud3.setRight(0);
    }

    public static void Draw(Canvas canvas, int cameraX)
    {
        //Background
        canvas.drawRect(0, 0, GameSurface.width, GameSurface.height *3/4, sky);

        //Sea (might remove)
        canvas.drawRect(0, GameSurface.height *3/4, GameSurface.width, GameSurface.height, sea);

        //Clouds & mountains
        cloud1.draw(canvas, -cameraX/12, 0);
        cloud2.draw(canvas, -cameraX/12, 0);

		for (DiPoint aMountain : mountain) aMountain.draw(canvas, -cameraX / 9, 0);

		cloud3.draw(canvas, -cameraX/6, 0);
    }

    public static void Materialize(int cameraX)
    {
        cloud1.setLeft(cloud1.getLeft() -cameraX/12);
        cloud2.setLeft(cloud2.getLeft() -cameraX/12);

		for (DiPoint aMountain : mountain) aMountain.setLeft(aMountain.getLeft() - cameraX / 9);

        cloud3.setLeft(cloud3.getLeft() -cameraX/6);
    }
}