package com.example.ghomi.chapter8_5;

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

public class MainActivity extends AppCompatActivity {
    EditText Name, Pass , updateold, updatenew, delete;
    myDbAdapter helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name= (EditText) findViewById(R.id.editName);
        Pass= (EditText) findViewById(R.id.editPass);
        updateold= (EditText) findViewById(R.id.curName);
        updatenew= (EditText) findViewById(R.id.newName);
        delete = (EditText) findViewById(R.id.delName);

        helper = new myDbAdapter(this);
    }
    public void addUser(View view)
    {
        String t1 = Name.getText().toString();
        String t2 = Pass.getText().toString();
        if(t1.isEmpty() || t2.isEmpty())
        {
            Toast.makeText(this, "Enter both name and pass", Toast.LENGTH_SHORT).show();
        }
        else
        {
            long id = helper.insertData(t1,t2);
            if(id<=0)
            {
                Toast.makeText(this, "Insertion Unsuccessful", Toast.LENGTH_SHORT).show();
                Name.setText("");
                Pass.setText("");
            } else
            {
                Toast.makeText(this, "Insertion successful", Toast.LENGTH_SHORT).show();
                Name.setText("");
                Pass.setText("");
            }
        }
    }

    public void viewData(View view)
    {
        String data = helper.getData();
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
    }

    public void update( View view)
    {
        String u1 = updateold.getText().toString();
        String u2 = updatenew.getText().toString();
        if(u1.isEmpty() || u2.isEmpty())
        {
            Toast.makeText(this, "Enter datal", Toast.LENGTH_SHORT).show();
        }
        else
        {
            int a= helper.updateName( u1, u2);
            if(a<=0)
            {
                Toast.makeText(this, "Unsuccessful", Toast.LENGTH_SHORT).show();
                updateold.setText("");
                updatenew.setText("");
            } else {

                Toast.makeText(this, "Updated", Toast.LENGTH_SHORT).show();
                updateold.setText("");
                updatenew.setText("");
            }
        }

    }
    public void delete( View view)
    {
        String uname = delete.getText().toString();
        if(uname.isEmpty())
        {
            Toast.makeText(this, "Enter data", Toast.LENGTH_SHORT).show();
        }
        else{
            int a= helper.delete(uname);
            if(a<=0)
            {
                Toast.makeText(this, " Unsuccessful", Toast.LENGTH_SHORT).show();
                delete.setText("");
            }
            else
            {
                Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
                delete.setText("");
            }
        }
    }
}
