package de.hofk.kreisberechnung.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.GridLayout;
import android.widget.TextView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class MainActivity extends ActionBarActivity {
    private int zaehle = 0 ;
    String radius_txt = "";  // Leerstring, falls sofort Berechnung ohne Eingabe
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridLayout gl = (GridLayout) findViewById(R.id.GridLayout1);
        gl.setColumnCount(2);
        gl.setRowCount(6);         // bei 5 crash
        // Edit 1   Zeile 0, Spalte 0
        GridLayout.LayoutParams lytp_edit1 = new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(0));
        final EditText edit1 = new EditText(this);
        edit1.setEms(4);
        edit1.setText(radius_txt);
        // für Softkeybord (wird nach Betätigung Button/Menü ausgeblendet - siehe dort
        final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        // Button 2   Zeile 1, Spalte 0
        GridLayout.LayoutParams lytp_button2 = new GridLayout.LayoutParams(GridLayout.spec(1), GridLayout.spec(0));
        final Button button2 = new Button(this);
        button2.setText(" Kreisfläche ");
        // Button 3   Zeile 2, Spalte 0
        GridLayout.LayoutParams lytp_button3 = new GridLayout.LayoutParams(GridLayout.spec(2), GridLayout.spec(0));
        final Button button3 = new Button(this);
        button3.setText("Kreisumfang");
        // tv 1  Zeile 1, Spalte 1   Zeile erstreckt sich über 3 Zeilen
        GridLayout.LayoutParams lytp_tv1 = new GridLayout.LayoutParams(GridLayout.spec(0, 3), GridLayout.spec(1));
        lytp_tv1.setGravity(Gravity.FILL);
        TextView tv1 = new TextView(this);
        tv1.setBackgroundColor(Color.argb(128, 50, 220, 220));
        tv1.setText("\nBitte den Radius eingeben.\n" +
                "Dann den entsrechenden\n" +
                "Button betätigen.\n" +
                "Kreisformeln:\n" +
                "Fläche     A = Pi*r*r\n" +
                "Umfang   U = 2*Pi*r\n");
        // tv 2  Zeile 3, Spalte 0
        GridLayout.LayoutParams lytp_tv2 = new GridLayout.LayoutParams(GridLayout.spec(3), GridLayout.spec(0));
        lytp_tv2.setGravity(Gravity.FILL);
        final TextView tv2 = new TextView(this);
        tv2.setText("Ergebnis\n");
        // Button 4   Zeile 3, Spalte 1
        GridLayout.LayoutParams lytp_button4 = new GridLayout.LayoutParams(GridLayout.spec(3), GridLayout.spec(1));
        final Button button4 = new Button(this);
        button4.setText("Klickzähler");
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button4.setText("Button " + (++zaehle) + " mal geklickt");
            }
        });
        // Canvas Zeile 4 über 2 Spalten
        GridLayout.LayoutParams lytp_canv = new GridLayout.LayoutParams(GridLayout.spec(5), GridLayout.spec(0, 2));
        CanvViev canv = new CanvViev(this);
        canv.setBackgroundColor(Color.rgb(240, 255, 160));
        // Button und  Textview mit Layout dem GridLayout übergeben
        gl.addView(edit1, lytp_edit1);
        gl.addView(button2, lytp_button2);
        gl.addView(button3, lytp_button3);
        gl.addView(tv2, lytp_tv2);
        gl.addView(button4, lytp_button4);
        gl.addView(tv1, lytp_tv1);
        gl.addView(canv, lytp_canv);
        edit1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tv2.setText(" ... tippt gerade ... ");
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                radius_txt = edit1.getText().toString();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imm.hideSoftInputFromWindow(edit1.getWindowToken(), 0); // Softkeybord ausblenden
                tv2.setText(berechne_flaeche(radius_txt));  // Kreisfläche berechnen
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imm.hideSoftInputFromWindow(edit1.getWindowToken(), 0); // Softkeybord ausblenden
                tv2.setText(berechne_umfang(radius_txt));   // Kreisumfang berechnen
            }
        });
    }
    //  onCreate(Bundle ...

    public class CanvViev extends View {
        Paint paint;
        public CanvViev(Context context) {
            super(context);
            paint = new Paint();
            paint.setColor(Color.BLUE);
            paint.setStyle(Paint.Style.STROKE);
        }
        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawCircle(200.0f, 100.0f, 80.0f, paint);
            canvas.drawCircle(150.0f, 250.0f, 120.0f, paint);
            canvas.drawCircle(350.0f, 450.0f, 200.0f, paint);
        }
    }
    public String berechne_flaeche(String kradius_txt) {     // Fläche berechnen

        if (radius_txt.matches("\\d+([.]{1}\\d+)?")) {
            double kradius = Double.parseDouble(kradius_txt);
            double flaeche = Math.PI * kradius * kradius;
            flaeche = Math.round(flaeche * 1000.0) / 1000.0;
            return "Ergebnis:\nFläche = " + flaeche;
        }
        else {
            return "keine Zahl!" ;
        }
    }
    public String berechne_umfang(String kradius_txt) {      // Umfang berechnen
        if (radius_txt.matches("\\d+([.]{1}\\d+)?")) {
            double kradius = Double.parseDouble(kradius_txt);
            double umfang = 2*Math.PI * kradius;
            umfang = Math.round(umfang * 1000.0) / 1000.0;
            return "Ergebnis:\nUmfang = " + umfang;
        }
        else {
            return "keine Zahl!" ;
        }
    }
    protected void ergebnisbox(String titel, String meldung) {
        new AlertDialog.Builder(this)
                .setMessage(meldung)
                .setTitle(titel)
                .setCancelable(true)
                .setNeutralButton(
                    android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton){}
                    }
                )
                .show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, Menu.NONE, "Fläche berechnen");
        menu.add(Menu.NONE, 2, Menu.NONE, "Umfang berechnen");
        menu.add(Menu.NONE, 3, Menu.NONE, "Programm beenden");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == 1 ) {
            ergebnisbox("Fläche",berechne_flaeche(radius_txt));  // Kreisfläche anzeigen
            return true;
        }
        if(item.getItemId() == 2 ) {
            ergebnisbox("Umfang",berechne_umfang(radius_txt)); // Kreisumfang anzeigen
            return true;
        }
        if(item.getItemId() == 3 ) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);   // oder nur  return false;
    }

}  //  MainActivity

