package com.example.dbsql05;



import android.app.Activity;
import android.app.AlertDialog;

// https://www.simplifiedcoding.net/android-sqlite-database-tutorial-insert-into-sqlite-db/

//https://www.simplifiedcoding.net/sqlite-database-in-android-using-sqliteopenhelper-class/ 

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
 
public class MainActivity extends Activity implements View.OnClickListener {
 
    private EditText editTextName;
    private EditText editTextAdd;
    private Button btnAdd;
    private Button btnView;
 
    private SQLiteDatabase db;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        createDatabase();
 
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextAdd = (EditText) findViewById(R.id.editTextAddress);
 
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnView = (Button) findViewById(R.id.btnView);
 
        btnAdd.setOnClickListener(this);
        btnView.setOnClickListener(this);
    }
 
 
    protected void createDatabase(){
        db=openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS persons(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,address VARCHAR);");
    }
 
    protected void insertIntoDB(){
        String name = editTextName.getText().toString().trim();
        String add = editTextAdd.getText().toString().trim();
        if(name.equals("") || add.equals("")){
            Toast.makeText(getApplicationContext(),"Please fill all fields", Toast.LENGTH_LONG).show();
            return;
        }
 
        String query = "INSERT INTO persons (name,address) VALUES('"+name+"', '"+add+"');";
        db.execSQL(query);
        Toast.makeText(getApplicationContext(),"Saved Successfully", Toast.LENGTH_LONG).show();
        
        
       // db.execSQL("INSERT INTO student VALUES('"+editRollno.getText()+"','"+
       // 		editName.getText()+"','"+editMarks.getText()+"');");

    }
 
    
    protected void saveRecord() 
    {
        String name = editTextName.getText().toString().trim();
        String add = editTextAdd.getText().toString().trim();
        String id = "5";

        String sql = "UPDATE persons SET name='" + name + "', address='" + add + "' WHERE id=" + id + ";";

        if (name.equals("") || add.equals("")) {
            Toast.makeText(getApplicationContext(), "You cannot save blank values", Toast.LENGTH_LONG).show();
            return;
        }

        db.execSQL(sql);
        Toast.makeText(getApplicationContext(), "Records Saved Successfully", Toast.LENGTH_LONG).show();
     
        //db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+
        //		editMarks.getText()+"' WHERE rollno='"+editRollno.getText()+"'");


        
        
    }
    
    private void deleteRecord() 
    {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Are you sure you want delete this person?");

        alertDialogBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1)
                    {
                        String id = "5";
                        String sql = "DELETE FROM persons WHERE id=" + id + ";";
                        db.execSQL(sql);
                        Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();
                        
                     //   db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");


                       
                    }
                });

        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1)
                    {

                    }
                });


        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

    }
 
    
    @Override
    public void onClick(View v)
    {
        if(v == btnAdd){
            insertIntoDB();
        }
    }
    
   // Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
   // if(c.moveToFirst())
   // {
   //     editName.setText(c.getString(1));
   //     editMarks.setText(c.getString(2));
   // }
    
}