package com.chexiang.myhome.base.adapter.table;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ListView;

import com.chexiang.myhome.utils.DensityUtils;

import java.util.Arrays;
import java.util.List;

import kale.adapter.item.AdapterItem;

/**
 * @Description:
 * @Copyright: Copyright (c) 2017 chexiang.com. All right reserved.
 * @Author: zhangshunjie
 * @Date: 2017/6/30 13:42
 * @Modifier: zhangshunjie
 * @Update: 2017/6/30 13:42
 */
public class SimpleTableAdapter extends CommonTableAdapter<List<String>> {

    protected int layoutResId;
    protected int headerLayoutResId;
    protected int[] resIds;
    protected int[] headerResIds;

    /**
     * @param data              数据
     * @param layoutResId       数据的布局的id
     * @param resIds            数据的布局的每个view的id
     * @param headers           header的数据
     * @param headerLayoutResId header的布局的id
     * @param headerResIds      header的布局的每个view的id
     * @param listView          用来更新listview高度
     * @param itemHeight        单个item的高度(单位dp)
     * @param activity
     */
    public SimpleTableAdapter(@Nullable List data, int layoutResId, int[] resIds,
                              String[] headers, int headerLayoutResId, int[] headerResIds,
                              ListView listView, int itemHeight, Activity activity) {
        super(data, 2);
        data.add(0, Arrays.asList(headers));
        setData(data);
        this.headerLayoutResId = headerLayoutResId;
        this.layoutResId = layoutResId;
        this.resIds = resIds;
        this.headerResIds = headerResIds;
        int heightDp = itemHeight * data.size() + (int) (data.size() * 1.2);
        listView.getLayoutParams().height = new DensityUtils(activity).dip2px(heightDp);
    }

    /**
     * @param data        数据
     * @param layoutResId 数据的布局的id
     * @param resIds      数据的布局的每个view的id
     * @param listView    用来更新listview高度
     * @param itemHeight  单个item的高度(单位dp)
     * @param activity
     */
    public SimpleTableAdapter(@Nullable List data, int layoutResId, int[] resIds,
                              ListView listView, int itemHeight, Activity activity) {
        super(data, 1);
        this.layoutResId = layoutResId;
        this.resIds = resIds;
        int heightDp = itemHeight * data.size() + (int) (data.size() * 1.2);
        listView.getLayoutParams().height = new DensityUtils(activity).dip2px(heightDp);
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object o) {
        if (o != null && (int) o == TYPE_HEADER) {
            return new SimpleTableHeaderItem(headerLayoutResId, headerResIds);
        } else {
            return new SimpleTableItem(layoutResId, resIds);
        }
    }

}