package net.yaht.m.iintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.widget.EditText;

import java.io.File;
import java.io.FileOutputStream;

// AppCompatActivity we do not use! {
public class AppHead extends Activity {

    public final static String EXTRA_MESSAGE = "net.yaht.m.iintent.MESSAGE";
    static final int PICK_ANSWER_REQUEST = 101;  // The request code

    EditText _edit_text = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_head);

        _edit_text = (EditText) findViewById(R.id._edit_text);
        _edit_text.setSelection(0);

    }

    public void _btn_send(View v) {

        Intent intent = new Intent(this, Show.class);

        EditText _edit_text = (EditText) findViewById(R.id._edit_text);

        String message = _edit_text.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);

        startActivity(intent);

    }

    public void _btn_get(View v) {

        Intent _pic_ans = new Intent(this, Ask.class);
        startActivityForResult(_pic_ans, PICK_ANSWER_REQUEST);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_ANSWER_REQUEST) {

            // If we have a result?
            if (resultCode == RESULT_OK) {

                _edit_text.setText(data.getStringExtra("ans"));

            }

        }

    }

    public void _btn_list(View v) {

        File _internal_dir = getFilesDir();
        File[] _flist = _internal_dir.listFiles();

        String _flist_txt = "";

        for (int i = 0; i < _flist.length; i++) {
            _flist_txt += i + ": " + _flist[i].getName() + "/" + _flist[i].length() + " bytes\n";
        }

        // _edit_text.setText(String.valueOf(_flist_txt.length()));
        _edit_text.setText(_flist_txt);

    }

    public void _btn_save(View v) {

        String filename = "file1.txt";
        String string = _edit_text.getText().toString();

        FileOutputStream outputStream;

        try {

            outputStream = openFileOutput(filename, MODE_PRIVATE);
            outputStream.write(string.getBytes());
            outputStream.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public void _btn_mem(View v) {


    }

}
