package com.imtm1.starwarsgame;


import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;



class JoyStickView  {

    float xDelta;
    float yDelta;
    float origineX;
    float origineY;
    ViewGroup mainLayout ;
    Context context;
    Boolean joystickIsPressed=false;
    ImageView tie;


    public JoyStickView (ViewGroup mainLayout, Context context, ImageView tie)
    {
        this.mainLayout = mainLayout;
        this.context = context;
        this.tie=tie;

    }

    final View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float halfPadSize = (v.getHeight()/2);
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    origineX=v.getX();
                    origineY=v.getY();

                    joystickIsPressed = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    xDelta =  event.getX() + v.getX() - halfPadSize;
                    yDelta = event.getY() + v.getY() - halfPadSize;

                    joystickIsPressed = true;
                    float displacement = (float) Math.sqrt(Math.pow(xDelta - origineX, 2) + Math.pow(yDelta - origineY, 2));

                    if(displacement > halfPadSize){ //si deplacement dans le cercle possible
                        xDelta=((xDelta-origineX)/displacement*(halfPadSize)+origineX);
                        yDelta=((yDelta-origineY)/displacement*(halfPadSize)+origineY);
                    }
                    v.setX(xDelta);
                    v.setY(yDelta);

                    //move vaisseau
                    tie.setX(tie.getX()+((xDelta - origineX)/20)); //on divise pour reduire la vitesse
                    tie.setY(tie.getY()+((yDelta - origineY)/20));

                    if(tie.getX()<0){
                        tie.setX(0);
                    }else if(tie.getX()>=(width-tie.getWidth())){
                        tie.setX(width-tie.getWidth());
                    }

                    if(tie.getY()<0){
                        tie.setY(0);
                    }else if(tie.getY()>=(height-v.getHeight()*3)){
                        tie.setY((float) (height-v.getHeight()*3));
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    v.setX(origineX);
                    v.setY(origineY);
                    joystickIsPressed = false;

                    break;
                default:
                    return false;
            }
            mainLayout.invalidate();

            return true;
        }
    };


}

