package pl.umk.fizyka.intencje;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.SmsManager;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import java.security.Permission;

public class MainActivity extends AppCompatActivity {

    static private boolean zdobądźUprawnienia(Activity activity, String permission, int requestCode)
    {
        //int permissionCheck = ContextCompat.checkSelfPermission(activity, android.Manifest.permission.READ_PHONE_STATE);
        int permissionCheck = ContextCompat.checkSelfPermission(activity, permission);
        if(permissionCheck != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(
                    activity,
                    new String[]{ permission },
                    //REQUEST_READ_PHONE_STATE);
                    requestCode);
            return false;
        }
        else return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Button b = findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                Button bv = (Button) view;
                bv.setText("Kliknięty");

                //Intent i = new Intent(MainActivity.this, DodatkowaActivity.class);
                //startActivity(i);

                //Intent i = new Intent(Intent.ACTION_VIEW);
                //i.setData(Uri.parse("http://www.fizyka.umk.pl/~jacek"));
                //startActivity(i);

                //Intent i = new Intent(Intent.ACTION_WEB_SEARCH);
                //i.putExtra(SearchManager.QUERY, "UMK");
                //startActivity(i);

                //String uri = "geo:53.012749,18.61278";
                //startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(uri)));

                /*
                try
                {
                    zdobądźUprawnienia(MainActivity.this, Manifest.permission.CALL_PHONE, 0);
                    Intent i = new Intent(Intent.ACTION_CALL);
                    i.setData(Uri.parse("tel:12345678"));
                    startActivity(i);
                }
                catch(SecurityException exc)
                {
                    Toast.makeText(getApplicationContext(), exc.getMessage(), Toast.LENGTH_LONG).show();
                }
                */

                //Intent i = new Intent(Intent.ACTION_DIAL);
                //i.setData(Uri.parse("tel:12345678"));
                //startActivity(i);

                /*
                try
                {
                    Intent i = new Intent(Intent.ACTION_DIAL);
                    i.setData(Uri.parse("tel:12345678"));
                    if(ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
                    {
                        throw new Exception("Nie uzyskano zgody");
                    }
                    else startActivity(i);
                }
                catch(Exception exc)
                {
                    Toast.makeText(getApplicationContext(), exc.getMessage(), Toast.LENGTH_LONG).show();
                }
                */

                //Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:1234567890"));
                //i.putExtra("sms_body", "Przykładowy SMS ąćśę");
                //startActivity(i);

                /*
                try
                {
                    zdobądźUprawnienia(MainActivity.this, Manifest.permission.SEND_SMS, 0);
                    SmsManager sms = SmsManager.getDefault();
                    PendingIntent pi = PendingIntent.getActivity(
                            getBaseContext(), 0,
                            new Intent(getBaseContext(), MainActivity.class),
                            0);
                    sms.sendTextMessage(
                            "1234567890", null, "Przykładowy SMS",
                            null, null);
                }
                catch(Exception exc)
                {
                    Toast.makeText(getApplicationContext(), exc.getMessage(), Toast.LENGTH_LONG).show();
                }
                */
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
