package com.halo.assistant.fragment.myconcern;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.gamecenter.common.callback.OnRequestCallBackListener;
import com.gh.common.util.CheckLoginUtils;
import com.gh.common.view.Concern_LinearLayout;
import com.gh.gamecenter.R;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.eventbus.EBConcernChanged;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.common.base.fragment.ToolbarFragment;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

/**
 * Created by CsHeng on 14/12/2017.
 * <p>
 * 我的关注grid页面
 */
public class MyConcernFragment extends ToolbarFragment implements OnRequestCallBackListener {

    private RecyclerView mConcernRv;
    private LinearLayout mNoneData;
    private LinearLayout mDataExceptionView;
    private TextView mNoDataTv;
    private LinearLayout mNoConn;

    private MyConcernAdapter concernAdapter;

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mConcernRv = view.findViewById(R.id.concern_rv_show);
        mNoneData = view.findViewById(R.id.reuse_none_data);
        mNoDataTv = view.findViewById(R.id.reuseNoneDataTv);
        mNoConn = view.findViewById(R.id.reuse_no_connection);
        mDataExceptionView = view.findViewById(R.id.reuse_data_exception);

        mNoDataTv.setOnClickListener(this);
        mConcernRv.setHasFixedSize(true);
        mConcernRv.setLayoutManager(new GridLayoutManager(getContext(), 3));
        concernAdapter = new MyConcernAdapter(getActivity(), this, mEntrance);
        mConcernRv.setAdapter(concernAdapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setNavigationTitle("我的关注");
    }

    @Override
    public void loadDone() { // 关注推荐 回调
        if (concernAdapter != null && concernAdapter.getItemCount() > 0) {
            ((Concern_LinearLayout) mCachedView).hideRecyclerViewNoAnimation();
        }
    }

    // 关注的游戏加载完成 再加载热门推荐(根据已关注的游戏过滤)
    @Override
    public void loadDone(Object obj) {
        mNoConn.setVisibility(View.GONE);
        mNoneData.setVisibility(View.GONE);
        if (obj == null) { // 未登录状态
            mNoneData.setVisibility(View.VISIBLE);
            mConcernRv.setVisibility(View.GONE);
            mNoDataTv.setText(R.string.login_hint);
            mNoDataTv.setTextColor(ContextCompat.getColor(getContext(), R.color.theme));
        } else {
            mConcernRv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void loadError() {
        mNoConn.setVisibility(View.VISIBLE);
        mNoneData.setVisibility(View.GONE);
    }

    @Override
    public void loadEmpty() {
        if (concernAdapter.getConcernGameList() == null
                || concernAdapter.getConcernGameList().isEmpty()) {
            mNoneData.setVisibility(View.VISIBLE);
            mConcernRv.setVisibility(View.GONE);
            mNoDataTv.setText(R.string.game_empty);
            mNoDataTv.setTextColor(ContextCompat.getColor(getContext(), R.color.c7c7c7));
        }
    }

    @Override
    public void loadNotFound() {
        mDataExceptionView.setVisibility(View.VISIBLE);
        mNoneData.setVisibility(View.GONE);
        mNoConn.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.reuseNoneDataTv) {
            String hint = getString(R.string.login_hint);
            if (hint.equals(mNoDataTv.getText().toString())) {
                CheckLoginUtils.checkLogin(getContext(), "我的关注-请先登录", null);
            }
        }
    }

    // 关注事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBConcernChanged changed) {
        if (!changed.isSingle()) {
            return;
        }
        if (changed.isConcern()) {
            boolean isExistConcern = false;
            for (GameEntity entity : concernAdapter.getConcernGameList()) {
                if (entity.getId().equals(changed.getGameId())) {
                    isExistConcern = true;
                    break;
                }
            }
            if (!isExistConcern) concernAdapter.initConcernGame();

        }
    }

    // 登录事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBReuse reuse) {
        if (reuse.getType().equals(LOGIN_TAG)) { // 登入
            concernAdapter = new MyConcernAdapter(getActivity(), this, mEntrance);
            mConcernRv.setAdapter(concernAdapter);
            mConcernRv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        if (concernAdapter != null) {
            mConcernRv.getRecycledViewPool().clear();
            concernAdapter.notifyItemRangeChanged(0, concernAdapter.getItemCount());
        }
    }
}
