package com.dlsu.getbetter.getbetter;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.dlsu.getbetter.getbetter.adapters.ExistingPatientAdapter;
import com.dlsu.getbetter.getbetter.database.DataAdapter;
import com.dlsu.getbetter.getbetter.objects.Patient;

import java.sql.SQLException;
import java.util.ArrayList;


public class ExistingPatientActivity extends AppCompatActivity implements View.OnClickListener {

    private DataAdapter getBetterDb;
    Button newPatientRecBtn;
    Button uploadPatientRecBtn;
    Button updatePatientRecBtn;
    Button createUpdateCaseRecBtn;

    private long selectedPatientId;

    private ArrayList<Patient> existingPatients;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_existing_patient);

        newPatientRecBtn = (Button)findViewById(R.id.create_new_patient_btn);
        uploadPatientRecBtn = (Button)findViewById(R.id.upload_patient_record);
        updatePatientRecBtn  = (Button)findViewById(R.id.update_patient_record_btn);
        createUpdateCaseRecBtn = (Button)findViewById(R.id.create_update_case_record_btn);

        RecyclerView existingPatientListView = (RecyclerView) findViewById(R.id.existing_patient_list);
        RecyclerView.LayoutManager existingPatientLayoutManager = new LinearLayoutManager(this);


        existingPatients = new ArrayList<>();
        initializeDatabase();
        new GetPatientListTask().execute(51);
        Log.e("patient size", existingPatients.size() + "");
        ExistingPatientAdapter existingPatientsAdapter = new ExistingPatientAdapter(existingPatients);

        existingPatientListView.setHasFixedSize(true);
        existingPatientListView.setLayoutManager(existingPatientLayoutManager);
        existingPatientListView.setAdapter(existingPatientsAdapter);
        existingPatientsAdapter.SetOnItemClickListener(new ExistingPatientAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                Log.e("Patient Click", existingPatients.get(position).getId() + "");
                selectedPatientId = existingPatients.get(position).getId();
            }
        });


        newPatientRecBtn.setOnClickListener(this);
        uploadPatientRecBtn.setOnClickListener(this);
        updatePatientRecBtn.setOnClickListener(this);
        createUpdateCaseRecBtn.setOnClickListener(this);
    }

    private void initializeDatabase () {

        getBetterDb = new DataAdapter(this);

        try {
            getBetterDb.createDatabase();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void getExistingPatients (int healthCenterId) {

        try {
            getBetterDb.openDatabase();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        existingPatients.addAll(getBetterDb.getPatients(healthCenterId));

        getBetterDb.closeDatabase();

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

        if (id == R.id.create_new_patient_btn) {

            Intent intent = new Intent(this, NewPatientRecordActivity.class);
            startActivity(intent);

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

            Intent intent = new Intent(this, UploadToServerActivity.class);
            startActivity(intent);

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

            Intent intent = new Intent(this, UpdatePatientRecordActivity.class);
            intent.putExtra("selectedPatient", selectedPatientId);
            startActivity(intent);

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

            Intent intent = new Intent(this, CreateUpdateCaseRecordActivity.class);
            intent.putExtra("selectedPatient", selectedPatientId);
            startActivity(intent);
        }
    }

    private class GetPatientListTask extends AsyncTask<Integer, Void, String> {

        private ProgressDialog progressDialog = new ProgressDialog(ExistingPatientActivity.this);

        @Override
        protected void onPreExecute () {
            super.onPreExecute();
            progressDialog.setMessage("Populating Patient List...");
            progressDialog.show();

        }

        @Override
        protected String doInBackground(Integer... params) {

            getExistingPatients(params[0]);

            return "Done!";
        }

        @Override
        protected void onPostExecute (String results) {

            super.onPostExecute(results);
            progressDialog.hide();
            progressDialog.dismiss();
        }
    }


}

@@ -931,17 +931,17 @@
 private 
-l
+L
 ong sele
@@ -954,16 +954,89 @@
 tientId;
+%0A    private String patientFirstName;%0A    private String patientLastName;
 %0A%0A    pr
@@ -1588,16 +1588,95 @@
 rd_btn);
+%0A        Button backBtn = (Button)findViewById(R.id.existing_patient_back_btn);
 %0A%0A      
@@ -2712,24 +2712,186 @@
 n).getId();%0A
+                patientFirstName = existingPatients.get(position).getFirstName();%0A                patientLastName = existingPatients.get(position).getLastName();%0A
             
@@ -3118,24 +3118,66 @@
 ener(this);%0A
+        backBtn.setOnClickListener(this);%0A
     %7D%0A%0A    p
@@ -4063,16 +4063,23 @@
 , Upload
+Patient
 ToServer
@@ -4431,32 +4431,194 @@
 _record_btn) %7B%0A%0A
+            if(selectedPatientId == null) %7B%0A                Toast.makeText(this, %22Please select a patient%22, Toast.LENGTH_LONG).show();%0A            %7D else %7B%0A%0A%0A    
             Inte
@@ -4677,32 +4677,36 @@
 ctivity.class);%0A
+    
             inte
@@ -4768,36 +4768,262 @@
         
-startActivity(intent
+    intent.putExtra(%22firstname%22, patientFirstName);%0A                intent.putExtra(%22lastname%22, patientLastName);%0A                startActivity(intent);%0A            %7D%0A%0A        %7D else if (id == R.id.existing_patient_back_btn) %7B%0A            finish(
 );%0A     

Add shared preference and back button function