package ru.unatco.rss.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import ru.unatco.rss.R;
import ru.unatco.rss.adapters.SubscriptionsAdapter;
import ru.unatco.rss.model.Subscription;


public class SubscriptionsActivity extends AppCompatActivity {

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(android.R.id.list)
    ListView mListView;
    @Bind(android.R.id.progress)
    ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscriptions);
        ButterKnife.bind(this);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Subscription sub = (Subscription) adapterView.getAdapter().getItem(i);
                Intent intent = new Intent(SubscriptionsActivity.this, FeedActivity.class);
                intent.putExtra(FeedActivity.ARG_SUB, sub);
                startActivity(intent);
            }
        });
        mListView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Subscription sub = new Subscription();
        sub.setmTitle("Радио-Т");
        sub.setmUrl("http://feeds.rucast.net/radio-t");
        List<Subscription> subs = new ArrayList<>();
        subs.add(sub);
        mListView.setAdapter(new SubscriptionsAdapter(getApplicationContext(), subs));
        mListView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.GONE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_subscriptions, 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);
    }
}

@@ -546,16 +546,113 @@
 dapter;%0A
+import ru.unatco.rss.data.local.RssDataStore;%0Aimport ru.unatco.rss.data.local.RssDatabaseHelper;%0A
 import r
@@ -750,16 +750,54 @@
 vity %7B%0A%0A
+    private RssDataStore mDataStore;%0A%0A
     @Bin
@@ -1744,24 +1744,93 @@
 ew.VISIBLE);
+%0A%0A        mDataStore = new RssDataStore(new RssDatabaseHelper(this));
 %0A    %7D%0A%0A    
@@ -1901,221 +1901,8 @@
 ();%0A
-        Subscription sub = new Subscription();%0A        sub.setmTitle(%22%D0%A0%D0%B0%D0%B4%D0%B8%D0%BE-%D0%A2%22);%0A        sub.setmUrl(%22http://feeds.rucast.net/radio-t%22);%0A        List%3CSubscription%3E subs = new ArrayList%3C%3E();%0A        subs.add(sub);%0A
     
@@ -1976,20 +1976,45 @@
 text(), 
-subs
+mDataStore.getSubscriptions()
 ));%0A    

use data from DB in SubscriptionsActivity