package com.gh.gamecenter.libao;

import static com.gh.gamecenter.common.constant.Constants.LIBAO_CHANGED_TAG;
import static com.gh.gamecenter.common.constant.Constants.LOGIN_TAG;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.gh.common.util.CheckLoginUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.base.fragment.BaseFragment;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.databinding.FragmentLibao3Binding;
import com.gh.gamecenter.eventbus.EBUISwitch;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by khy on 2016/12/12.
 */
public class Libao3Fragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private FragmentLibao3Binding mBinding;

    private LinearLayoutManager mLayoutManager;
    private Libao3FragmentAdapter adapter;
    private String mFilter = "expires:false";

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            adapter = new Libao3FragmentAdapter(getActivity(), Libao3Fragment.this, mFilter, mEntrance);
            mBinding.libao3RvList.setAdapter(adapter);
            adapter.loadData();
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_libao3;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = FragmentLibao3Binding.bind(mCachedView);

        mBinding.libao3SrlRefresh.setColorSchemeResources(com.gh.gamecenter.common.R.color.primary_theme);
        mBinding.libao3SrlRefresh.setOnRefreshListener(this);
        mBinding.reuseNoConnection.getRoot().setOnClickListener(this);
        mBinding.reuseNoneData.reuseNoneDataTv.setOnClickListener(this);

        mBinding.libao3RvList.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mBinding.libao3RvList.setLayoutManager(mLayoutManager);
        adapter = new Libao3FragmentAdapter(getActivity(), this, mFilter, mEntrance);
        mBinding.libao3RvList.addItemDecoration(getItemDecoration());
        mBinding.libao3RvList.setAdapter(adapter);

        mBinding.libao3RvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && mLayoutManager.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) {
                    if (!adapter.isOver() && !adapter.isLoading() && !adapter.isNetworkError()) {
                        adapter.loadData();
                    }
                }
            }
        });
        mBinding.radiogroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.receivedRb) {
                mFilter = "expires:false";
            } else {
                mFilter = "expires:true";
            }
            postRunnable(runnable);
        });
    }

    private RecyclerView.ItemDecoration getItemDecoration() {
        Drawable insetDivider = ContextCompat.getDrawable(requireContext(), com.gh.gamecenter.common.R.drawable.divider_item_line_space_16);
        CustomDividerItemDecoration itemDecoration = new CustomDividerItemDecoration(requireContext(), false, false, true, false);
        itemDecoration.setDrawable(insetDivider);
        return itemDecoration;
    }

    @Override
    public void loadDone() { // 数据加载成功回调
        mBinding.libao3SrlRefresh.setRefreshing(false);
        mBinding.reuseLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.GONE);
        mBinding.radiogroup.setVisibility(View.VISIBLE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.GONE);
    }

    @Override
    public void loadEmpty() { // 数据为空回调
        mBinding.libao3SrlRefresh.setRefreshing(false);
        mBinding.reuseLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.VISIBLE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.GONE);
    }

    @Override
    public void loadError() {
        super.loadError();
        mBinding.libao3SrlRefresh.setRefreshing(false);
        mBinding.reuseLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.VISIBLE);
    }

    @Override
    public void loadDone(Object obj) {
        super.loadDone(obj);
        mBinding.libao3SrlRefresh.setRefreshing(false);
        mBinding.reuseLoading.getRoot().setVisibility(View.GONE);
        mBinding.radiogroup.setVisibility(View.GONE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.VISIBLE);
        mBinding.reuseNoneData.reuseNoneDataTv.setText(com.gh.gamecenter.feature.R.string.login_hint);
        mBinding.reuseNoneData.reuseNoneDataTv.setTextColor(ContextCompat.getColor(getContext(), com.gh.gamecenter.common.R.color.text_theme));
    }

    public void onClick(View v) { // 重新连接
        switch (v.getId()) {
            case R.id.reuse_no_connection:
                mBinding.libao3SrlRefresh.setRefreshing(true);
                mBinding.libao3RvList.setVisibility(View.VISIBLE);
                mBinding.reuseLoading.getRoot().setVisibility(View.VISIBLE);
                mBinding.reuseNoConnection.getRoot().setVisibility(View.GONE);
                postDelayedRunnable(runnable, 1000);
                break;
            case R.id.reuseNoneDataTv:
                String hint = getString(com.gh.gamecenter.feature.R.string.login_hint);
                if (hint.equals(mBinding.reuseNoneData.reuseNoneDataTv.getText().toString())) {
                    CheckLoginUtils.checkLogin(getContext(), "礼包中心-存号箱-请先登录", null);
                }
                break;
        }
    }

    @Override
    public void onRefresh() {
        postDelayedRunnable(runnable, 1000);
    }

    // 资讯Fragment界面切换事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBUISwitch busNine) {
        if (LibaoFragment.EB_LIBAOACTIVITY_TAG.equals(busNine.getFrom())) {
            if (busNine.getPosition() == 2 && mBinding.reuseLoading.getRoot().getVisibility() == View.VISIBLE) {
                adapter.loadData();
            }
        }
    }

    // 礼包数据库数量改变事件/登录事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBReuse changed) {
        if (LIBAO_CHANGED_TAG.equals(changed.getType())) {
            postRunnable(runnable);
        } else if (LOGIN_TAG.equals(changed.getType())) { // 登入
            mBinding.reuseNoneData.reuseNoneDataTv.setText(com.gh.gamecenter.common.R.string.game_empty);
            mBinding.reuseNoneData.reuseNoneDataTv.setTextColor(ContextCompat.getColor(getContext(), com.gh.gamecenter.common.R.color.c7c7c7));
            postRunnable(runnable);
        }
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        ExtensionsKt.setRootBackgroundColor(mBinding.getRoot(), com.gh.gamecenter.common.R.color.ui_surface);
        mBinding.radiogroup.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface));
        mBinding.receivedRb.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.bg_button_round_selector));
        mBinding.receivedRb.setTextColor(ContextCompat.getColorStateList(requireContext(), R.color.libao_rg_button_selector));
        mBinding.expiredRb.setBackground(ContextCompat.getDrawable(requireContext(), R.drawable.bg_button_round_selector));
        mBinding.expiredRb.setTextColor(ContextCompat.getColorStateList(requireContext(), R.color.libao_rg_button_selector));
        mBinding.libao3RvList.getRecycledViewPool().clear();
        adapter.notifyItemRangeChanged(0, adapter.getItemCount());
        if (mBinding.libao3RvList.getItemDecorationCount() > 0) {
            mBinding.libao3RvList.removeItemDecorationAt(0);
            mBinding.libao3RvList.addItemDecoration(getItemDecoration());
        }
    }
}
