package com.sheep.gamegroup.module.find.fragment;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.LongSparseArray;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sheep.gamegroup.absBase.AbsApiRefresh;
import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.absBase.CheckErrorApiRefresh;
import com.sheep.gamegroup.absBase.IApiRefresh;
import com.sheep.gamegroup.model.entity.Lp;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.util.viewHelper.LayoutParamsUtil;
import com.sheep.gamegroup.view.fragment.BaseListFragment6;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;

import cn.finalteam.rxgalleryfinal.bean.MediaBean;
import cn.finalteam.rxgalleryfinal.utils.MediaUtils;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/11/27.
 * realicing@sina.com
 * 小绵羊3.4.5新增--发布发现内容--选择图片
 */
public class FgtMediaPickerImg extends BaseListFragment6<MediaBean> {

    @Override
    public int getLayoutId() {
        return R.layout.common_srl_rv;
    }

    @Override
    public void initView() {
        LayoutParamsUtil.resetLayoutParams(recyclerView, new Lp(G.DENSITY).setTbMargin(14).setLrMargin(6));
    }

    protected LongSparseArray<MediaBean> sparseArray = new LongSparseArray<>();
    protected LongSparseArray<String> textSparseArray = new LongSparseArray<>();

    private int width = (G.WIDTH - G.getRealPix(48)) / 2;

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    }

    @Override
    protected BaseQuickAdapter<MediaBean, BaseViewHolder> getAdapter() {
        return new BaseQuickAdapter<MediaBean, BaseViewHolder>(R.layout.item_media_picker, apiRefresh.getList()) {

            @Override
            protected void convert(BaseViewHolder helper, MediaBean item) {
                boolean isChoose = sparseArray.get(item.getId()) != null;
                helper.setText(R.id.item_media_picker_tv, isChoose ? textSparseArray.get(item.getId()) : "√");
                helper.setBackgroundRes(R.id.item_media_picker_tv, isChoose ? R.drawable.shape_oval_solid_10_main : R.drawable.shape_oval_solid_10_f5);
                helper.setTextColor(R.id.item_media_picker_tv, SheepApp.getInstance().getResources().getColor(isChoose ? R.color.white : R.color.gray_CBCBCB));
                ImageView item_media_picker_iv = helper.getView(R.id.item_media_picker_iv);
                ViewUtil.setViewWH(item_media_picker_iv, width, item.getHeight() * 1.0f / item.getWidth());
                ViewUtil.setImagePath(item_media_picker_iv, item.getOriginalPath(), G.getRealPix(5));
            }
        };
    }

    private AbsApiRefresh<MediaBean> apiRefresh;

    @Override
    protected void addApiRefresh(List<IApiRefresh> apiRefreshList) {
        apiRefresh = new CheckErrorApiRefresh<MediaBean>(this) {
            @Override
            protected boolean isRightData(MediaBean item) {
                return item != null && item.getWidth() > 0 && item.getHeight() > 0;
            }
            @Override
            public void initData() {
                Observable.create(new ObservableOnSubscribe<List<MediaBean>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<MediaBean>> emitter) {
                        emitter.onNext(initNewList(page, per_page));
                    }
                })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()).
                        subscribe(new AbsObserver<List<MediaBean>>() {
                            @Override
                            public void onNext(List<MediaBean> newList) {
                                loadList(newList);
                            }
                        });
            }
        };
        apiRefreshList.add(apiRefresh);
    }

    protected List<MediaBean> initNewList(int page, int per_page) {
        return MediaUtils.getMediaWithImageList(SheepApp.getInstance(), page, per_page);
    }
}