package com.kfzs.cfyl.media.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckedTextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kfzs.cfyl.media.BaseFragment;
import com.kfzs.cfyl.media.R;
import com.kfzs.cfyl.media.api.Levelble;
import com.kfzs.cfyl.media.bean.Lp;
import com.kfzs.cfyl.media.util.G;
import com.kfzs.cfyl.media.util.LayoutParamsUtil;
import com.kfzs.cfyl.media.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by realicing on 2018/12/11.
 * realicing@sina.com
 * 磨皮级别选择列表
 */
public class FgtChooseLevelList extends BaseFragment {
    @Override
    public int getLayoutId() {
        return R.layout.media_fgt_choose_level_list;
    }

    @Override
    public void onViewCreated() {
        initView();
        initListener();
        initData();
    }


    RecyclerView recyclerView;

    private void initView() {
        recyclerView = findViewById(R.id.recyclerView);
    }


    private int selectPosition = 0;

    private void initListener() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new BaseQuickAdapter<Integer, BaseViewHolder>(R.layout.media_item_level, list) {
            @Override
            protected void convert(BaseViewHolder helper, final Integer item) {
                CheckedTextView item_level_ctv = helper.getView(R.id.item_level_ctv);
                switch (item) {
                    case 0:
                        LayoutParamsUtil.resetLayoutParams(item_level_ctv, new Lp(per).setLeftMargin(ps).setRightMargin(p));
                        break;
                    default:
                        LayoutParamsUtil.resetLayoutParams(item_level_ctv, new Lp(per).setRightMargin(p));
                        break;
                    case count - 1:
                        LayoutParamsUtil.resetLayoutParams(item_level_ctv, new Lp(per).setRightMargin(ps));
                        break;
                }
                item_level_ctv.setChecked(item == selectPosition);
                ViewUtil.setText(item_level_ctv, String.valueOf(item));
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        selectPosition = item;
                        ViewUtil.notifyDataSetChanged(recyclerView);
                        if (getActivity() instanceof Levelble) {
                            ((Levelble) getActivity()).setLevel(item);
                        }
                    }
                });
            }
        });
    }

    private List<Integer> list = new ArrayList<>();

    private float per;//每份距离
    private int ps = 3;//左右的距离级别
    private int p = 2;//中间的距离级别
    private final int count = 6;

    private void initData() {
        for (int i = 0; i < count; i++) {
            list.add(i);
        }
        ViewUtil.notifyDataSetChanged(recyclerView);
        per = (G.WIDTH - count * G.DENSITY * 40) / (ps * 2 + (count - 1) * p);
    }

}
