package com.gh.gamecenter.collection;

import static com.gh.common.constant.Config.URL_ARTICLE;

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

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.gh.common.util.CollectionUtils;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.NewsDetailActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.WebActivity;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.common.baselist.ListFragment;
import com.gh.gamecenter.common.baselist.LoadType;
import com.gh.gamecenter.common.baselist.NormalListViewModel;
import com.gh.gamecenter.common.entity.ToolBoxEntity;
import com.gh.gamecenter.eventbus.EBCollectionChanged;
import com.gh.gamecenter.login.user.UserManager;
import com.gh.gamecenter.retrofit.RetrofitManager;

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

import java.util.List;

import io.reactivex.Observable;

/**
 * Created by khy on 18/07/17.
 */

public class ToolsFragment extends ListFragment<ToolBoxEntity, NormalListViewModel> {

    private ToolsAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCachedView.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white));
    }

    @Override
    protected ListAdapter provideListAdapter() {
        return mAdapter == null ? mAdapter = new ToolsAdapter(getContext(), this) : mAdapter;
    }

    @Override
    public Observable<List<ToolBoxEntity>> provideDataObservable(int page) {
        return RetrofitManager.getInstance().getApi().getCollectionTools(UserManager.getInstance().getUserId(), page);
    }

    // 收藏事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBCollectionChanged changed) {
        if (changed.getCollectionType().equals(CollectionUtils.CollectionType.TOOLKIT)) {
            mListViewModel.load(LoadType.REFRESH);
        }
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        ToolBoxEntity toolBoxEntity = (ToolBoxEntity) data;
        String url = toolBoxEntity.getUrl();
        if (url != null && url.contains(URL_ARTICLE)) {
            String newsId = url.substring(url.lastIndexOf("/") + 1, url.length() - 5);
            Intent intent = NewsDetailActivity.getIntentById(getContext(), newsId, true, "(收藏:工具箱)");
            startActivity(intent);
        } else {
            startActivity(WebActivity.getWebByCollectionTools(getContext(), toolBoxEntity, true));
        }
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        Drawable insetDivider = ContextCompat.getDrawable(requireContext(), R.drawable.divider_item_line_space_16);
        CustomDividerItemDecoration itemDecoration = new CustomDividerItemDecoration(requireContext(), false, false, true, false);
        itemDecoration.setDrawable(insetDivider);
        return itemDecoration;
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        mCachedView.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white));
        if (mListRv != null && mListRv.getItemDecorationCount() > 0) {
            mListRv.removeItemDecorationAt(0);
            mListRv.addItemDecoration(getItemDecoration());
        }
    }
}
