package com.hyperling.apps.games;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

/**
 * Created by ling on 12/22/16.
 */

public abstract class Player extends GameObject {

    private String TAG;

    //private Bitmap spritesheet;
    private int score;

    //private boolean up, down, left, right, sprint;
    private boolean playing;
    private Animation animation;
    private long startTime;

    public Player(Bitmap b, int w, int h, int numFrames){
        TAG = GameView.getTAG();

        setX(GameView.WIDTH/5);
        setY(GameView.HEIGHT/2);
        setDx(0);
        setDy(0);

        score = 0;

        setWidth(w);
        setHeight(h);

        Bitmap[] image = new Bitmap[numFrames];
        animation = new Animation();

        for (int i = 0; i < numFrames; i++){
            image[i] = Bitmap.createBitmap(b, width * i, 0, width, height);
        }

        animation.setFrames(image);
        animation.setDelay(100);
        startTime = System.currentTimeMillis();
    }

    public Player(){
        super();
    }

    /*
    public void setUp(boolean b){
        up = b;
    }

    public void setDown(boolean b){
        down = b;
    }

    public void setLeft(boolean b){
        left = b;
    }

    public void setRight(boolean b){
        right = b;
    }

    public void setSprint(boolean b){
        sprint = b;
    }
    */

    public boolean isPlaying(){
        return playing;
    }

    public void setPlaying(boolean p){
        playing = p;
    }

    public int getScore() {
        return score;
    }

    public void update(){
        animation.update();

        long elapsed = (System.currentTimeMillis() - startTime);
        if (elapsed > 100){
            score++;
            startTime = System.currentTimeMillis();
        }

        /* Specific animals should override this
        if (left){
            dx = Math.abs(dx) * -1;
        }
        else if (right){
            dx = Math.abs(dx);
        }

        if (sprint){
            dx *= 2;
        }

        if (up){
            dy += -1;
        }
        else{
            dy += 2;
        }
        if (dy < -5){
            dy = -5;
        }
        if (dy > 10){
            dy = 10;
        }
        */

        /* Character specific as well
        // Only move when going up or have not hit the ground yet.
        if (y < GameView.HEIGHT - getHeight() || dy < 0) {
            y += dy;
        }
        // Do not leave bottom of map
        else{
            y = GameView.HEIGHT - getHeight();
            dy = 0;
        }

        // Do not leave top of map
        if (y < 0){
            y = 0;
        }
        */

        setEndX(getX() + getWidth());
        setCenterX((getX() + getEndX()) / 2);

        setBottomY(getY() + getHeight());
        setCenterY((getY() + getBottomY()) / 2);

        Log.d(TAG, "Player.update():" +
                "  score=" + score +
                ", x=" + x +
                ", dx=" + dx +
                ", y=" + y +
                ", dy=" + dy);
    }

    public void draw(Canvas canvas){
        canvas.drawBitmap(animation.getImage(), getX(), getY(), null);
    }
}
