package com.example.kraft_300.bottomnavigationaddsubmultdiv;

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


/**
 * Created by Kraft-300 on 31.03.2017.
 */

public class SqrFragment extends Fragment {

    private TextView edNumber1 = null;
    private EditText edNumber3 = null;
    private EditText edComment = null;
    private Button bnTesten = null;
    private Button bnAgain = null;
    private int zahl1=0;
    private int ergebnis=0;

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

        bnTesten = (Button) view.findViewById(R.id.bntesten);
        bnTesten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bnTesten_click();
            }
        });
        bnAgain = (Button) view.findViewById(R.id.bnagain);
        bnAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bnAgain_click();
            }
        });
        setRandomNumbers();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        //TextView tv = (TextView) getView();
        //tv.setText(getString(R.string.text1));
    }

    private void setRandomNumbers() {
        Random rand = new Random();
        this.zahl1 = rand.nextInt(100) + 1;
        this.ergebnis = this.zahl1*this.zahl1;
        edNumber1.setText(Integer.toString(zahl1));
        edNumber3.setText("????");
        edComment.setText("");
        Log.e("Zahl1",Integer.toString(zahl1));
    }
    private void bnAgain_click() {
        setRandomNumbers();
    }

    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;
            return objects;
        } catch (NumberFormatException e) {
            objects[0]=false;
            objects[1]=0;
            return objects;
        }
    }

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

    }  // bnTesten_click


}
