package com.example.kraft_300.bottomnavigationaddsubmultdiv;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.Random;

/**
 * Created by Intel NUC on 13.04.2017.
 */

public class AddFragment extends Fragment {

    private TextView edNumber1 = null;
    private TextView edNumber2 = null;
    private EditText edNumber3 = null;
    private EditText edComment = null;

    private Button bnTesten=null;
    private Button bnAgain=null;
    private int zahl1=0;
    private int zahl2=0;
    private int ergebnis=0;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle saveInstanceState
    ) {
        View view = inflater.inflate(
                R.layout.addlayout, container, false);
        edNumber1 = (TextView) view.findViewById(R.id.ednumber1);
        edNumber2 = (TextView) view.findViewById(R.id.ednumber2);
        edNumber3 = (EditText) view.findViewById(R.id.ednumber3);
        edComment = (EditText) view.findViewById(R.id.edcomment);

        edNumber1.setText("12");

        bnTesten = (Button) view.findViewById(R.id.bntesten);
        bnAgain = (Button) view.findViewById(R.id.bnagain);



        bnTesten.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bnTest_click();
                    }
                }
        );
        bnAgain.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bnAgain_click();
                    }
                }
        );

        setRandomNumbers();
        return view;
    } // onCreate

    private void setRandomNumbers() {
        Random rand = new Random();
        this.zahl1 = rand.nextInt(500)+1;
        this.zahl2 = rand.nextInt(500)+1;
        this.ergebnis = this.zahl1+this.zahl2;
        this.edNumber1.setText(Integer.toString(this.zahl1));
        this.edNumber2.setText(Integer.toString(this.zahl2));
        this.edNumber3.setText("");
        this.edComment.setText("");
        Log.e("Zahl1",Integer.toString(this.zahl1));
        Log.e("Zahl2",Integer.toString(this.zahl2));
    }

    // Rückgabe a) Eingabe okay  b) eingegebene Zahl
    private Object[] getIntNumber(EditText edText) {
        Object[] objects = new Object[2];
        try {
            String s = edText.getText().toString();
            int zahl = Integer.parseInt(s);
            objects[0] = true;
            objects[1] = zahl;
        }
        catch (NumberFormatException e) {
            objects[0] = false;
            objects[1] = -1;
        }
        return objects;
    }

    private void bnTest_click(){
        Object[] values;
        values = getIntNumber(edNumber3);
        if (!  ((Boolean)values[0])    ) {
            edComment.setText(getString(R.string.wrongnumberformat));
            return;
        }
        int ergebnis = ((Integer)values[1]).intValue();
        if (ergebnis==this.ergebnis) {
            edComment.setText(getString(R.string.correctnumber));
        }
        else {
            edComment.setText(getString(R.string.wrongnumber));
        }
    } // bnTest_click

    private void bnAgain_click() {
        setRandomNumbers();
    }

}
