package com.stylepoints.habittracker.viewmodel.CentralHubActivity;

import android.arch.lifecycle.LiveData;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.stylepoints.habittracker.R;
import com.stylepoints.habittracker.repository.DatabaseInitUtil;
import com.stylepoints.habittracker.repository.HabitRepository;
import com.stylepoints.habittracker.repository.local.AppDatabase;
import com.stylepoints.habittracker.repository.local.entity.HabitEntity;
import com.stylepoints.habittracker.viewmodel.HabitEventRelatedActivites.EventsMainActivity;
import com.stylepoints.habittracker.viewmodel.HabitListViewModel;
import com.stylepoints.habittracker.viewmodel.HabitRelatedActivities.HabitsMainActivity;

import java.util.List;


public class MainActivity extends AppCompatActivity {
    private AppDatabase db;

    Button habitButton;
    Button eventButton;
    Button profileButton;
    Button socialButton;

    ListView listView;

    private LiveData<List<HabitEntity>> habitList;
    private HabitRepository repo;

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


        AppDatabase db = AppDatabase.getAppDatabase(getApplicationContext());
        // for now, initialize database with a couple of test entries
        DatabaseInitUtil.initializeDbWithTestData(db);

        repo = HabitRepository.getInstance(db);
        habitList = repo.loadAll();

        bindToUi();
        subscribeToModel();
    }

    private void subscribeToModel() {
        habitList.observe(this, habitList -> {
            if (habitList == null) { return; }
            // TODO: do this a better way
            for (HabitEntity habit : habitList) {
                if (!habit.isActiveToday()) {
                    habitList.remove(habit);
                }
            }

            ArrayAdapter<HabitEntity> adapter = new ArrayAdapter<HabitEntity>(
                    this, android.R.layout.simple_list_item_1, habitList);
            listView.setAdapter(adapter);
        });
    }

    private void bindToUi() {
        listView = (ListView) findViewById(R.id.todaysHabitsList);
        habitButton = (Button) findViewById(R.id.habitsMenuButton);
        eventButton = (Button) findViewById(R.id.eventsMenuButton);
        profileButton = (Button) findViewById(R.id.profileMenuButton);
        socialButton = (Button) findViewById(R.id.socialMenuButton);

        eventButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, EventsMainActivity.class);
                startActivity(intent);
            }
        });

        habitButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, HabitsMainActivity.class);
                startActivity(intent);
            }
        });
    }
}

@@ -884,24 +884,52 @@
 nActivity;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -1147,16 +1147,96 @@
 stView;%0A
+    ArrayAdapter%3CHabitEntity%3E adapter;%0A%0A    private List%3CHabitEntity%3E habitList;
 %0A    pri
@@ -1268,21 +1268,20 @@
 ntity%3E%3E 
-habit
+full
 List;%0A  
@@ -1633,16 +1633,19 @@
 %0A       
+ //
  Databas
@@ -1737,29 +1737,28 @@
 b);%0A        
-habit
+full
 List = repo.
@@ -1767,16 +1767,55 @@
 adAll();
+%0A        habitList = new ArrayList%3C%3E();
 %0A%0A      
@@ -1851,24 +1851,183 @@
 beToModel();
+%0A%0A        adapter = new ArrayAdapter%3CHabitEntity%3E(%0A                this, android.R.layout.simple_list_item_1, habitList);%0A        listView.setAdapter(adapter);
 %0A    %7D%0A%0A    
@@ -2064,29 +2064,28 @@
 ) %7B%0A        
-habit
+full
 List.observe
@@ -2091,21 +2091,20 @@
 e(this, 
-habit
+full
 List -%3E 
@@ -2121,21 +2121,20 @@
     if (
-habit
+full
 List == 
@@ -2193,16 +2193,47 @@
 ter way%0A
+            habitList.clear();%0A
         
@@ -2261,21 +2261,20 @@
 habit : 
-habit
+full
 List) %7B%0A
@@ -2297,9 +2297,8 @@
 if (
-!
 habi
@@ -2352,14 +2352,11 @@
 ist.
-remove
+add
 (hab
@@ -2388,25 +2388,24 @@
           %7D%0A
-%0A
             
@@ -2408,189 +2408,37 @@
     
-ArrayAdapter%3CHabitEntity%3E adapter = new ArrayAdapter%3CHabitEntity%3E(%0A                    this, android.R.layout.simple_list_item_1, habitList);%0A            listView.setAdapter(adapter
+adapter.notifyDataSetChanged(
 );%0A 

fix #41