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.AppController;
import com.gh.base.BaseFragment;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.eventbus.EBNetworkState;
import com.gh.gamecenter.eventbus.EBUISwitch;

/**
 * Created by khy on 2016/8/15.
 */
public class News4Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    public static final String TAG = News4Fragment.class.getSimpleName();

    private SwipeRefreshLayout news_swipe_refresh;
    private RecyclerView news_rv_show;
    private News4FragmentAdapter adapter;
    private LinearLayoutManager layoutManager;
    private LinearLayout reuse_no_connection;
    private LinearLayout reuse_none_data;
    private ProgressBarCircularIndeterminate news_pb_loading;

    private boolean isEverpause;

    private Handler handler = new Handler();

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

        init(R.layout.fragment_news4);

        isEverpause = false;

        news_rv_show.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        news_rv_show.setLayoutManager(layoutManager);
        adapter = new News4FragmentAdapter(this, false);
        news_rv_show.setAdapter(adapter);
        news_rv_show.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && layoutManager.findLastVisibleItemPosition() == adapter
                            .getConcernList().size()) {
                    if (!adapter.isRemove() && !adapter.isLoading()) {
                        adapter.loadDataByKey(adapter.getConcernList().size());
                    }
                }
            }
        });

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

        reuse_no_connection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                news_rv_show.setVisibility(View.VISIBLE);
                reuse_no_connection.setVisibility(View.GONE);
                news_pb_loading.setVisibility(View.VISIBLE);
                handler.postDelayed(runnable, 1000);
            }
        });
    }

    // 关注事件
    public void onEventMainThread(EBConcernChanged changed) {
        if (reuse_none_data.getVisibility() == View.VISIBLE) {
            reuse_none_data.setVisibility(View.GONE);
        }
        news_swipe_refresh.setRefreshing(true);
        news_rv_show.setVisibility(View.VISIBLE);
        news_pb_loading.setVisibility(View.VISIBLE);
        adapter = new News4FragmentAdapter(News4Fragment.this, true);
        news_rv_show.setAdapter(adapter);
    }

    //Fragment界面切换事件
    public void onEventMainThread(EBUISwitch swith) {
        if ("NewsFragment".equals(swith.getFrom())) {
            if (swith.getPosition() == 3) {
                if (news_pb_loading.getVisibility() == View.VISIBLE) {
                    adapter.loadDataByKey(0);
                }
            }
        }
    }

    //连接上网络事件
    public void onEventMainThread(EBNetworkState busNetworkState) {
        if (busNetworkState.isNetworkConnected()) {
            if (reuse_no_connection.getVisibility() == View.VISIBLE) {
                news_swipe_refresh.setRefreshing(true);
                news_rv_show.setVisibility(View.VISIBLE);
                news_pb_loading.setVisibility(View.GONE);
                reuse_no_connection.setVisibility(View.GONE);
                handler.postDelayed(runnable, 1000);
            } else if (adapter.isNetworkError()) {
                adapter.setNetworkError(false);
                adapter.notifyItemChanged(adapter.getItemCount() - 1);
                adapter.loadDataByKey(adapter.getConcernList().size());
            }
        }
    }

    public boolean isEverpause() {
        return isEverpause;
    }

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

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

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            adapter = new News4FragmentAdapter(News4Fragment.this, true);
            news_rv_show.setAdapter(adapter);
        }
    };

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

    @Override
    public void loadDone() {
        if (news_swipe_refresh != null && news_swipe_refresh.isRefreshing()) {
            news_swipe_refresh.setRefreshing(false);
        }
        if (news_pb_loading != null && news_pb_loading.getVisibility() == View.VISIBLE) {
            news_pb_loading.setVisibility(View.GONE);
        }
        if (news_rv_show != null && news_rv_show.getVisibility() == View.GONE) {
            news_rv_show.setVisibility(View.VISIBLE);
        }
        if (reuse_none_data.getVisibility() == View.VISIBLE) {
            reuse_none_data.setVisibility(View.GONE);
        }
        if (news_swipe_refresh != null) {
            news_swipe_refresh.setEnabled(true);
        }
        if (adapter != null
                && (adapter.getConcernList() == null
                || adapter.getConcernList().isEmpty())) {
            reuse_none_data.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void loadError() {
        if (news_swipe_refresh != null && news_swipe_refresh.isRefreshing()) {
            news_swipe_refresh.setRefreshing(false);
        }
        if (news_pb_loading != null && news_pb_loading.getVisibility() == View.VISIBLE) {
            news_pb_loading.setVisibility(View.GONE);
        }
        if (reuse_none_data.getVisibility() == View.VISIBLE){
            reuse_none_data.setVisibility(View.GONE);
        }
        if (news_swipe_refresh != null){
            news_swipe_refresh.setEnabled(false);
        }
        news_rv_show.setVisibility(View.GONE);
        reuse_no_connection.setVisibility(View.VISIBLE);
    }

    @Override
    public void loadEmpty() {
        if (news_pb_loading.getVisibility() == View.VISIBLE) {
            news_pb_loading.setVisibility(View.GONE);
        }
        if (news_swipe_refresh != null){
            news_swipe_refresh.setEnabled(false);
        }
        news_rv_show.setVisibility(View.GONE);
        reuse_none_data.setVisibility(View.VISIBLE);
        news_swipe_refresh.setRefreshing(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AppController.canclePendingRequests(TAG);
    }
}
