package it.andoma.legocarcontroller;

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.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by mling_000 on 17/08/2016.
 */
public class DrawView extends View {
    private Paint paint = new Paint();

    private boolean firstTime = true;
    private float[] x = new float[4];
    private float[] y = new float[4];

    private MainActivity ma;

    private float temperature = (float)0.0;

    private String lastSent = "0";
    private String command = "0";
    private int gear = 1;


    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
        paint.setTextSize(50);
        paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
        ma = (MainActivity) context;
    }

    @Override
    public void onDraw(Canvas canvas) {
        if(firstTime) {
            float step = canvas.getWidth()/3;
            x[0] = 0;
            x[3] = canvas.getWidth();
            x[1] = x[3]/3;
            x[2] = x[1]*2;

            float halfH = canvas.getHeight()/2;
            y[1] = halfH - (step/2);
            y[2] = halfH + (step/2);
            y[0] = y[1] - step;
            y[3] = y[2] + step;

            firstTime = false;
        }
        canvas.drawLine(x[1], y[0], x[1], y[3], paint);
        canvas.drawLine(x[2], y[0], x[2], y[3], paint);
        canvas.drawLine(x[0], y[1], x[3], y[1], paint);
        canvas.drawLine(x[0], y[2], x[3], y[2], paint);

        //icona sx
        RectF rectL = new RectF(x[0], y[1], x[1], y[2]-1);
        Bitmap bmL = BitmapFactory.decodeResource(getResources(), R.drawable.left);
        canvas.drawBitmap(bmL, null, rectL, null);

        //icona dx
        RectF rectR = new RectF(x[2], y[1], x[3], y[2]-1);
        Bitmap bmR = BitmapFactory.decodeResource(getResources(), R.drawable.right);
        canvas.drawBitmap(bmR, null, rectR, null);

        //icona marcia +
        RectF rectU = new RectF(x[1], y[0], x[2], y[1]-1);
        Bitmap bmU = BitmapFactory.decodeResource(getResources(), R.drawable.up);
        canvas.drawBitmap(bmU, null, rectU, null);

        //icona indietro
        RectF rectB = new RectF(x[1], y[2], x[2], y[3]);
        Bitmap bmB = BitmapFactory.decodeResource(getResources(), R.drawable.back);
        canvas.drawBitmap(bmB, null, rectB, null);

        //icona stop
        RectF rectS = new RectF(x[1], y[1], x[2], y[2]-1);
        Bitmap bmS = BitmapFactory.decodeResource(getResources(), R.drawable.stop);
        canvas.drawBitmap(bmS, null, rectS, null);

        //icona follower
        RectF rectF = new RectF(x[0], y[2], x[1], y[3]);
        Bitmap bmF = BitmapFactory.decodeResource(getResources(), R.drawable.follower);
        canvas.drawBitmap(bmF, null, rectF, null);

        //icona non-follower
        RectF rectN = new RectF(x[2], y[2], x[3], y[3]);
        Bitmap bmN = BitmapFactory.decodeResource(getResources(), R.drawable.nonfollower);
        canvas.drawBitmap(bmN, null, rectN, null);


        temperature = ConnectionManager.getInstance().getTempSens();

        canvas.drawText("Temperatura: " + Float.toString(temperature) + "°C", 10, 50, paint);
        canvas.drawText("Azione: " + lastSent, 10, 110, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            if(event.getX() > x[0] && event.getX() < x[1]) {
                if(event.getY() > y[0] && event.getY() < y[1]) {
                    //non serve
                }
                else if(event.getY() >= y[1] && event.getY() < y[2]) {
                    command = "l";
                    Log.d("legocar", command);
                }
                else if(event.getY() >= y[2] && event.getY() < y[3]) {
                    command = "a";
                    Log.d("legocar", command);
                }
            }
            else if(event.getX() >= x[1] && event.getX() < x[2]) {
                if(event.getY() > y[0] && event.getY() < y[1]) {
                    command = "1";
                    Log.d("legocar", command);
                }
                else if(event.getY() >= y[1] && event.getY() < y[2]) {
                    command = "0";
                    Log.d("legocar", command);
                }
                else if(event.getY() >= y[2] && event.getY() < y[3]) {
                    command = "9";
                    Log.d("legocar", command);
                }
            }
            else if(event.getX() >= x[2] && event.getX() < x[3]) {
                if(event.getY() > y[0] && event.getY() < y[1]) {
                    //non serve
                }
                else if(event.getY() >= y[1] && event.getY() < y[2]) {
                    command = "r";
                    Log.d("legocar", command);
                }
                else if(event.getY() >= y[2] && event.getY() < y[3]) {
                    command = "b";
                    Log.d("legocar", command);
                }
            }
        }

        if(command.equals("1")) {
            gear++;
            if(gear == 6) gear = 1;
            command = Integer.toString(gear);
        }

        ma.send(command);

        lastSent = command;

        postInvalidate();

        return super.onTouchEvent(event);
    }
}
