package com.example.ghomi.chapter8_1;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    EditText editname, editpass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editname = (EditText)findViewById(R.id.editName);
        editpass = (EditText)findViewById(R.id.editPass);
    }

    public void save(View v){
        File file = null;
        String name = editname.getText().toString();
        String password = editpass.getText().toString();
        FileOutputStream output = null;
        try {
            name += " ";
            file = getFilesDir();
            output = openFileOutput("code.txt", Context.MODE_PRIVATE);
            output.write(name.getBytes());
            output.write(password.getBytes());
            Toast.makeText(this, "Saved \n" + "Path -- " + file + "\t code.txt", Toast.LENGTH_SHORT).show();
            editname.setText(" ");
            editpass.setText(" ");

        }catch(Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                output.close();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    public void next (View view) {
        Toast.makeText(this, "Next", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, RetreiveActivity.class);
        startActivity(intent);
    }
}
