package com.dlsu.getbetter.getbetter;

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

import com.dlsu.getbetter.getbetter.database.DataAdapter;
import com.dlsu.getbetter.getbetter.objects.HealthCenter;

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


public class HomeActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    private DataAdapter getBetterDb;
    private RecyclerView urgentCaseRecyclerList;
    private RecyclerView addIntCaseRecyclerList;
    private RecyclerView closedCaseRecyclerList;
    private Spinner healthCenterSpinner;
    private int healthCenterId;
    private String healthCenterName;
    private ArrayList<HealthCenter> healthCenters;
    private ArrayAdapter<CharSequence> healthCenterAdapter;


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

        //Button changeHealthCenterBtn = (Button)findViewById(R.id.change_health_center_btn);
        urgentCaseRecyclerList = (RecyclerView)findViewById(R.id.urgent_case_records);
        addIntCaseRecyclerList = (RecyclerView)findViewById(R.id.additional_instructions_case_records);
        closedCaseRecyclerList = (RecyclerView)findViewById(R.id.closed_case_records);
        healthCenterSpinner = (Spinner)findViewById(R.id.health_center_spinner);



        Button viewCreatePatientBtn = (Button)findViewById(R.id.view_create_patient_records_btn);
        Button downloadAdditionalContentBtn = (Button)findViewById(R.id.download_content_btn);

        viewCreatePatientBtn.setOnClickListener(this);
        downloadAdditionalContentBtn.setOnClickListener(this);
        //changeHealthCenterBtn.setOnClickListener(this);
        healthCenterSpinner.setOnItemSelectedListener(this);


        healthCenters = new ArrayList<>();
        initializeDatabase();
        getHealthCenter();
        String healthCenterArray [] = new String[healthCenters.size()] ;
        for(int i = 0; i < healthCenters.size(); i++) {

            healthCenterArray[i] = healthCenters.get(i).getHealthCenterName();

        }

        healthCenterAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, healthCenterArray);

        healthCenterId = healthCenters.get(0).getHealthCenterId();
        healthCenterName = healthCenters.get(0).getHealthCenterName();
        healthCenterSpinner.setAdapter(healthCenterAdapter);


    }

    public void initializeDatabase () {

        getBetterDb = new DataAdapter(this);

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

    public void getHealthCenter() {

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

        healthCenters.addAll(getBetterDb.getHealthCenters());

        getBetterDb.closeDatabase();

    }

    public int getHealthCenterId (String healthCenterName) {

        int id = 0;
        try {
            getBetterDb.openDatabase();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        id = getBetterDb.getHealthCenterId(healthCenterName);

        return id;
    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

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

            Intent intent = new Intent(this, ExistingPatientActivity.class);
            startActivity(intent);
        } else if (id == R.id.download_content_btn) {


        }
    }

    public void downloadContent () {

        class DownloadContentTask extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {


            }

            @Override
            protected Void doInBackground(Void... params) {
                return null;
            }
        }

         new DownloadContentTask().execute();
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        healthCenterName = parent.getItemAtPosition(position).toString();
        healthCenterId = getHealthCenterId(healthCenterName);
        Log.e("health center id", healthCenterId + "");
        Toast.makeText(HomeActivity.this, healthCenterName + "", Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

        healthCenterName = parent.getSelectedItem().toString();
        healthCenterId = getHealthCenterId(healthCenterName);
    }
}

@@ -538,24 +538,98 @@
 ealthCenter;
+%0Aimport com.dlsu.getbetter.getbetter.sessionmanagers.SystemSessionManager;
 %0A%0Aimport jav
@@ -842,196 +842,8 @@
 Db;%0A
-    private RecyclerView urgentCaseRecyclerList;%0A    private RecyclerView addIntCaseRecyclerList;%0A    private RecyclerView closedCaseRecyclerList;%0A    private Spinner healthCenterSpinner;%0A
     
@@ -966,60 +966,47 @@
     
-private ArrayAdapter%3CCharSequence%3E healthCenterAdapt
+SystemSessionManager systemSessionManag
 er;%0A
@@ -1273,16 +1273,29 @@
         
+RecyclerView 
 urgentCa
@@ -1317,32 +1317,33 @@
 = (RecyclerView)
+ 
 findViewById(R.i
@@ -1374,16 +1374,29 @@
         
+RecyclerView 
 addIntCa
@@ -1418,32 +1418,33 @@
 = (RecyclerView)
+ 
 findViewById(R.i
@@ -1492,16 +1492,29 @@
         
+RecyclerView 
 closedCa
@@ -1544,16 +1544,17 @@
 lerView)
+ 
 findView
@@ -1585,32 +1585,40 @@
 cords);%0A        
+Spinner 
 healthCenterSpin
@@ -1632,16 +1632,17 @@
 Spinner)
+ 
 findView
@@ -1676,16 +1676,78 @@
 nner);%0A%0A
+        systemSessionManager = new SystemSessionManager(this);
 %0A%0A      
@@ -2493,32 +2493,59 @@
       %7D%0A%0A       
+ ArrayAdapter%3CCharSequence%3E
  healthCenterAda
@@ -2855,18 +2855,16 @@
 apter);%0A
-%0A%0A
     %7D%0A%0A 
@@ -3443,20 +3443,16 @@
   int id
- = 0
 ;%0A      
@@ -3637,24 +3637,62 @@
 nterName);%0A%0A
+        getBetterDb.closeDatabase();%0A%0A
         retu
@@ -3837,24 +3837,125 @@
 rds_btn) %7B%0A%0A
+            systemSessionManager.setHealthCenter(healthCenterName, String.valueOf(healthCenterId));%0A%0A
             
@@ -4015,24 +4015,25 @@
 ity.class);%0A
+%0A
             
@@ -4055,16 +4055,17 @@
 ntent);%0A
+%0A
         
@@ -4107,24 +4107,227 @@
 ent_btn) %7B%0A%0A
+            systemSessionManager.setHealthCenter(healthCenterName, String.valueOf(healthCenterId));%0A%0A            Intent i = new Intent(this, DownloadContentActivity.class);%0A%0A            startActivity(i);
 %0A        %7D%0A 

Add health center session id