package com.github.mzule.androidweekly.ui.activity;

import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.github.mzule.androidweekly.R;
import com.github.mzule.androidweekly.api.ApiCallback;
import com.github.mzule.androidweekly.api.ArticleApi;
import com.github.mzule.androidweekly.entity.Article;
import com.github.mzule.androidweekly.entity.Issue;
import com.github.mzule.androidweekly.ui.adapter.ArticleAdapter;
import com.github.mzule.androidweekly.ui.adapter.SlideAdapter;
import com.github.mzule.androidweekly.ui.view.ProgressView;

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

import butterknife.Bind;
import butterknife.OnItemClick;

public class MainActivity extends BaseActivity {

    @Bind(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @Bind(R.id.listView)
    ListView listView;
    @Bind(R.id.progressView)
    ProgressView progressView;
    @Bind(R.id.slideListView)
    ListView slideListView;
    private ArticleAdapter adapter;
    private SlideAdapter slideAdapter;
    private List<Issue> issues;

    @OnItemClick(R.id.slideListView)
    void onSlideItemClick(AdapterView<?> parent, View view, int position, long id) {
        Issue issue = (Issue) parent.getAdapter().getItem(position);
        active(issue);
        slideAdapter.notifyDataSetChanged();

        drawerLayout.closeDrawers();
        progressView.start();
        new ArticleApi().getPage(issue.getName(), new ApiCallback<List<Object>>() {
            @Override
            public void onSuccess(List<Object> data) {
                adapter.clear();
                adapter.addAndNotify(data);
                progressView.finish();
            }

            @Override
            public void onFailure(Exception e) {
            }
        });
    }

    @OnItemClick(R.id.listView)
    void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Article article = (Article) parent.getAdapter().getItem(position);
        startActivity(ArticleActivity.makeIntent(this, article));
    }

    @Override
    protected void afterInject() {
        adapter = new ArticleAdapter(this);
        listView.setAdapter(adapter);

        slideAdapter = new SlideAdapter(this);
        slideListView.setAdapter(slideAdapter);

        sendRequest();
    }


    private void active(Issue issue) {
        for (Issue i : issues) {
            i.setActive(i == issue);
        }
    }

    private void sendRequest() {
        new ArticleApi().getPage(new ApiCallback<List<Object>>() {
            @Override
            public void onSuccess(List<Object> data) {
                adapter.clear();
                adapter.addAndNotify(data);
                progressView.finish();

                issues = new ArrayList<>();
                for (int i = 197; i > 0; i--) {
                    issues.add(new Issue("issue-" + i, i == 197));
                }
                slideAdapter.clear();
                slideAdapter.addAndNotify(issues);
            }

            @Override
            public void onFailure(Exception e) {
                progressView.finish();
            }
        });
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_main;
    }
}

@@ -1382,24 +1382,63 @@
 Changed();%0A%0A
+        sendRequest(issue.getName());%0A%0A
         draw
@@ -1496,397 +1496,8 @@
 ();%0A
-        new ArticleApi().getPage(issue.getName(), new ApiCallback%3CList%3CObject%3E%3E() %7B%0A            @Override%0A            public void onSuccess(List%3CObject%3E data) %7B%0A                adapter.clear();%0A                adapter.addAndNotify(data);%0A                progressView.finish();%0A            %7D%0A%0A            @Override%0A            public void onFailure(Exception e) %7B%0A            %7D%0A        %7D);%0A
     
@@ -2007,16 +2007,20 @@
 Request(
+null
 );%0A    %7D
@@ -2177,16 +2177,28 @@
 Request(
+String issue
 ) %7B%0A    
@@ -2226,16 +2226,23 @@
 getPage(
+issue, 
 new ApiC
@@ -2441,32 +2441,116 @@
     
-progressView.finish();%0A%0A
+listView.setSelection(0);%0A                progressView.finish();%0A%0A                if (issues == null) %7B%0A    
     
@@ -2585,24 +2585,28 @@
 ayList%3C%3E();%0A
+    
             
@@ -2637,24 +2637,28 @@
 %3E 0; i--) %7B%0A
+    
             
@@ -2720,34 +2720,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2784,32 +2784,36 @@
                 
+    
 slideAdapter.add
@@ -2827,24 +2827,42 @@
 fy(issues);%0A
+                %7D%0A
             

switch issue with scroll listview to top