package com.gh.gamecenter.news;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
import com.gh.base.BaseFragment;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBUISwitch;

/**
 * Created by khy on 2016/6/29.
 */
public class News2Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    public static final String TAG = News2Fragment.class.getSimpleName();
    private SwipeRefreshLayout original_swipe_refresh;
    private RecyclerView original_list;
    private LinearLayoutManager layoutManager;
    private News2FragmentAdapter adapter;
    private ProgressBarCircularIndeterminate original_pb_loading;
    private LinearLayout reuse_no_connection;

    private boolean isEverpause;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init(R.layout.fragment_original);

        isEverpause = false;

        original_swipe_refresh.setColorSchemeResources(R.color.theme);
        original_swipe_refresh.setOnRefreshListener(this);

        reuse_no_connection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                original_swipe_refresh.setRefreshing(true);
                original_list.setVisibility(View.VISIBLE);
                original_pb_loading.setVisibility(View.VISIBLE);
                reuse_no_connection.setVisibility(View.GONE);
                handler.postDelayed(runnable, 1000);
            }
        });

        original_list.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        original_list.setLayoutManager(layoutManager);
        adapter = new News2FragmentAdapter(News2Fragment.this, false);
        original_list.setAdapter(adapter);
        original_list.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (!isDestroy && newState == RecyclerView.SCROLL_STATE_IDLE
                        && layoutManager.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) {
                    if (!adapter.isRemove() && !adapter.isLoading() && !adapter.isNetworkError()) {
                        adapter.addList(adapter.getNewsList().size());
                    }
                }
            }
        });
    }

    @Override
    public void loadDone() {
        if (original_swipe_refresh != null && original_swipe_refresh.isRefreshing()) {
            original_swipe_refresh.setRefreshing(false);
        }
        if (original_pb_loading != null && original_pb_loading.getVisibility() == View.VISIBLE) {
            original_pb_loading.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadError() {
        if (original_swipe_refresh != null && original_swipe_refresh.isRefreshing()) {
            original_swipe_refresh.setRefreshing(false);
        }
        if (original_pb_loading != null && original_pb_loading.getVisibility() == View.VISIBLE) {
            original_pb_loading.setVisibility(View.GONE);
        }
        original_list.setVisibility(View.GONE);
        reuse_no_connection.setVisibility(View.VISIBLE);
    }

    //连接上网络事件
    public void onEventMainThread(EBNetworkState busNetworkState) {
        if (busNetworkState.isNetworkConnected()) {
            if (reuse_no_connection.getVisibility() == View.VISIBLE) {
                original_swipe_refresh.setRefreshing(true);
                original_list.setVisibility(View.VISIBLE);
                original_pb_loading.setVisibility(View.GONE);
                reuse_no_connection.setVisibility(View.GONE);
                handler.postDelayed(runnable, 1000);
            }
        }
    }

    //Fragment界面切换事件
    public void onEventMainThread(EBUISwitch busNine) {
        if ("NewsFragment".equals(busNine.getFrom())) {
            if (busNine.getPosition() == 1) {
                if (original_pb_loading.getVisibility() == View.VISIBLE) {
                    adapter.load();
                }
            }
        }
    }

    private Handler handler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (!isDestroy) {
                adapter = new News2FragmentAdapter(News2Fragment.this, true);
                original_list.setAdapter(adapter);
            }
        }
    };

    @Override
    public void onRefresh() {
        handler.postDelayed(runnable, 1000);
    }

    public boolean isEverpause() {
        return isEverpause;
    }

    @Override
    public void onPause() {
        super.onPause();
        isEverpause = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        isEverpause = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        original_swipe_refresh = null;
        original_list = null;
        layoutManager = null;
        adapter = null;
        original_pb_loading = null;
        reuse_no_connection = null;
        handler = null;
    }
}
