package com.imtm1.starwarsgame;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Path;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.PathInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageView padExte;
    ImageView padCenter;
    ViewGroup mainLayout;
    ImageView asteroid1;
    ImageView asteroid2;
    ImageView tie;

    @SuppressLint("ClickableViewAccessibility")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        padExte = findViewById(R.id.padExte);
        padCenter = findViewById(R.id.padCenter);
        mainLayout = (RelativeLayout) findViewById(R.id.main);
        asteroid1 = findViewById(R.id.asteroid1);
        asteroid2 = findViewById(R.id.asteroid2);
        tie = findViewById(R.id.tie);

        Path path = new Path();
        path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
        ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, path);
        animator.setDuration(2000);
        animator.setRepeatCount(Animation.INFINITE);
        animator.start();
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(asteroid2, View.X, View.Y, path);
        animator2.setDuration(5000);
        animator2.setRepeatCount(Animation.INFINITE);
        animator2.start();


        JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this,tie);

        Handler handlerForMovingTie = new Handler();
        Runnable movingTie = new Runnable() {
            @Override
            public void run() {
               // tie.setTranslationX(joyStickView.xDelta);
               // tie.setTranslationY(joyStickView.yDelta);
                Log.i( "TAG","Bool:"+ joyStickView.joystickIsPressed);
                if (joyStickView.joystickIsPressed) {
                    Toast.makeText(MainActivity.this, "joysitck is pressed", Toast.LENGTH_SHORT).show();
                    handlerForMovingTie.postDelayed(this, 10);
                    mainLayout.invalidate();

                }
            }
        };
        handlerForMovingTie.post(movingTie);
        Thread threadTie = new Thread(movingTie);
        threadTie.start();
        Log.i("TAG", "Thread:"+Thread.currentThread().getName());
        Log.i("tag", "Hello");
        padCenter.setOnTouchListener(joyStickView.touchListener);

    }
}
