package pb.project.picamera;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.support.v7.app.ActionBarActivity;
import android.text.InputType;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private static final String PREFERENCES_NAME = "piCamPref";
	private static final String KEY_IP = "KEY_IP";
	
	private Socket socket;
	private static final int SERVERPORT = 5000;

	private String ipAddress;
	SharedPreferences preferences;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		preferences = getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		ipAddress = preferences.getString(KEY_IP, null);
		
		
		if (ipAddress == null){
			showIpAddressDialog();
		}else
			connect();
		
		//webView.loadUrl("http://"+"www.google.com");
	}
	
	private void saveIpAddress(){
		SharedPreferences.Editor ed = preferences.edit();
		ed.putString(KEY_IP, ipAddress);
		ed.commit();
	}

	private void connect(){
		Intent i = new Intent(this, CommService.class);
		i.putExtra("ipAddr", ipAddress);
		
		if(!mIsBound)
		startService(i);
		doBindService();

		WebView webView = (WebView) findViewById(R.id.webView1);
		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webView.setWebViewClient(new WebViewClient());
		webView.loadUrl("http://"+ipAddress+":8082");
	}
	

	
	CommService mBoundService;
	private boolean mIsBound = false;
	
	private ServiceConnection mConnection = new ServiceConnection() {
        //EDITED PART
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
		     mBoundService = ((CommService.LocalBinder)service).getService();
		
		}
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
		     mBoundService = null;
		}
		
	};
		
		
	private void doBindService() {
		Intent i = new Intent(this, CommService.class);
		i.putExtra("ipAddr", ipAddress);
	
	   bindService(i, mConnection, Context.BIND_AUTO_CREATE);
	   mIsBound = true;
	   if(mBoundService!=null){
	   }
	}
		
		
	private void doUnbindService() {
	   if (mIsBound) {
	       // Detach our existing connection.
	       unbindService(mConnection);
	       mIsBound = false;
	   }
	}
	
	
	private void showIpAddressDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Zadej IP adresu");
		
		final EditText nametext = new EditText(this);
		nametext.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
		nametext.setHint("10.0.0.1");
		builder.setView(nametext)
			.setPositiveButton("OK", 
					new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String newName = nametext.getText().toString();
					ipAddress = newName;
					saveIpAddress();
					connect();
				}
			});
		builder.create().show();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.change_ip) {
			showIpAddressDialog();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
