package net.testing.databasemanipulation.feature;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    DBManipulate dbManipulate = new DBManipulate(this, "elevi.db", null, 1);
    private ListView lv;
    DBAdapter dbAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

/*
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
*/

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        dbManipulate.createTblElevi();

        populateList();

        Button btnInsert = findViewById(R.id.btnInsert);
        Button btnUpdate = findViewById(R.id.btnUpdate);
        Button btnDelete = findViewById(R.id.btnDelete);

        //passing listener object to button
        btnInsert.setOnClickListener(this.btnInsertFunction);
        btnUpdate.setOnClickListener(this.btnUpdateFunction);
        btnDelete.setOnClickListener(this.btnDeleteFunction);


    }


    private View.OnClickListener btnInsertFunction = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Snackbar.make(v, "ADAUGAT", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

            EditText id = findViewById(R.id.editTextID);
            EditText nume = findViewById(R.id.editTextNume);
            EditText materie = findViewById(R.id.editTextMaterie);
            EditText nota = findViewById(R.id.editTextNota);

            String ids = id.getText().toString();
            String numes = nume.getText().toString();
            String materies = materie.getText().toString();
            String notas = nota.getText().toString();
            Float notaf = Float.parseFloat(nota.getText().toString());

            dbManipulate.addElev(numes,materies,notaf);

            populateList();

        }
    };

    private View.OnClickListener btnDeleteFunction = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Snackbar.make(v, "STERS", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

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


            Long idl = Long.parseLong(id.getText().toString());


            dbManipulate.deleteElev(idl);

            populateList();

        }
    };


    private View.OnClickListener btnUpdateFunction = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Snackbar.make(v, "ACTUALIZAT", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

            EditText id = findViewById(R.id.editTextID);
            EditText nume = findViewById(R.id.editTextNume);
            EditText materie = findViewById(R.id.editTextMaterie);
            EditText nota = findViewById(R.id.editTextNota);

            Integer idi = Integer.parseInt(id.getText().toString());
            Long idl = Long.parseLong(id.getText().toString());
            String numes = nume.getText().toString();
            String materies = materie.getText().toString();
            String notas = nota.getText().toString();
            Float notaf = Float.parseFloat(nota.getText().toString());

            dbManipulate.updateElev(idl,numes,materies,notaf);

            populateList();

        }
    };

    public void populateList(){
        JSONArray dbResults = dbManipulate.getElevi();
        dbAdapter = new DBAdapter(getApplicationContext(), R.layout.db_row_layout);
        lv = (ListView) findViewById(R.id.listviewDB);
        try {
            for (int i = 0; i < dbResults.length(); i++) {
                JSONObject jsonobject = dbResults.getJSONObject(i);
                //Log.d("JsonObject", String.valueOf(jsonobject));
                String ID = jsonobject.getString("ID");
                String Nume = jsonobject.getString("Nume");
                String Materie = jsonobject.getString("Materie");
                String Nota = jsonobject.getString("Nota");

                DBGetSet dbGetSet = new DBGetSet(ID,Nume,Materie,Nota);
                dbAdapter.add(dbGetSet);
            }

            lv.setAdapter(dbAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            Intent intentMeniu = new Intent(this, ActivityDB.class);
            startActivity(intentMeniu);
        } else if (id == R.id.nav_gallery) {


        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
