package com.utbm.lo53.whereami;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.util.Log;
import android.widget.Toast;

public class Calibration extends AppCompatActivity
{
    ImageView dot;
    float x,y;
    String address;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calibration);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        dot = (ImageView)findViewById(R.id.dot);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.r_lay_cal);
        relativeLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int action = event.getAction();
                x = (int)event.getX();
                y = (int)event.getY();
                if (action == MotionEvent.ACTION_DOWN)
                {
                    TranslateAnimation animation = new TranslateAnimation(0, x - dot.getX(), 0, y - dot.getY() );
                    animation.setDuration(1000);
                    animation.setFillAfter(false);
                    animation.setAnimationListener(new MyAnimationListener());
                    dot.startAnimation(animation);
                }
                return false;
            }
        });
        x = dot.getX();
        y = dot.getY();
    }
    private class MyAnimationListener implements Animation.AnimationListener
    {
        @Override
        public void onAnimationEnd(Animation animation)
        {
            dot.clearAnimation();
            dot.setX(x);
            dot.setY(y);
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
        }
        @Override
        public void onAnimationStart(Animation animation) {
        }
    }

    public void onClickSend (View view)
    {
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        if(wifiManager.isWifiEnabled())
        {
            // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
            WifiInfo info = wifiManager.getConnectionInfo();
            address = info.getMacAddress();
        }
        else
        {
            // ENABLE THE WIFI FIRST
            wifiManager.setWifiEnabled(true);
            // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
            WifiInfo info = wifiManager.getConnectionInfo();
            address = info.getMacAddress();
        }
        //check the preferences to get ip/port
        SharedPreferences sharedPref = getSharedPreferences("preferences", MODE_PRIVATE);
        String ip_address = sharedPref.getString("ip_address", "");
        String port_number = sharedPref.getString("port_number", "");
        if (ip_address == "" || port_number == "")
        {
            Toast.makeText(getApplicationContext(),"You need to set Server IP/Port", Toast.LENGTH_LONG).show();
            //open Preference activity
            Intent intent = new Intent(this, Preference.class);
            startActivity(intent);
        }
        else
        {
            // send request to server
            Connection.command(ip_address, port_number,"/calibration/setPosition?mac_addr="+ address + "&posx=" + x + "&posy=" + y);
            Toast.makeText(getApplicationContext(),"Sent successfully", Toast.LENGTH_LONG).show();
            Log.d("debugPos","x= "+x+" y= "+y);
        }
    }

}

