package com.tofly.ghrq_route.common.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.esri.arcgisruntime.data.Field;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/1/15.
 */

public class PipeCalloutViewAdapter extends BaseAdapter {
    private Map<String, Object> mAttrsMap;
    private List<Field> mFieldList;
    private Context mContext;
    private List<String> mNameList = new ArrayList<>();
    private List<String> mValueList = new ArrayList<>();
    private static String[] dates = {"BUILDDATA", "CHANGEDATE", "FINISHDATE", "GASDATE", "WRITEDATE",
            "FACTORYDATE", "RUNDATE", "REPAIRDATE"};
    private static List<String> dateCorrect = Arrays.asList(dates);


    public PipeCalloutViewAdapter(Context context, Map<String, Object> attrs, List<Field> fields) {
        mContext = context;
        mAttrsMap = attrs;
        mFieldList = fields;
        initData();
    }


    private void initData() {
        if (mAttrsMap != null && mAttrsMap.size() > 0) {
            for (Field field : mFieldList) {
                String fieldname = field.getName();
                if (mAttrsMap.containsKey(fieldname)) {
                    String aliasName = field.getAlias();
                    mNameList.add(aliasName);
                    Object value = mAttrsMap.get(fieldname);
                    String strValue = "";
                    if (value != null) {
                        if (dateCorrect.contains(fieldname)){
                            strValue = DateUtils.getLongTime(value.toString());
                        }else {
                            strValue = value.toString();
                        }
                    }
                    mValueList.add(strValue);
                }
            }
        }
    }

    /*private String getKeyAliasFromFileds(String name) {
        String alias = name;
        if (mFieldList != null && mFieldList.size() > 0) {
            for (Field field : mFieldList) {
                String fieldName = field.getName();
                if (fieldName.equalsIgnoreCase(name)) {
                    alias = field.getAlias();
                    break;
                }
            }
        }
        return alias;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_callout_pipe, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String strName = mNameList.get(position);
        if (strName != null)
            holder.attrNameText.setText(strName);

        String strValue = mValueList.get(position);
        if (strValue != null) {
            holder.attrValueText.setText(strValue);
        }
    }

    @Override
    public int getItemCount() {
        return mAttrsMap.size();
    }*/

    @Override
    public int getCount() {
        return mAttrsMap.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_callout_pipe, parent);
            viewHolder = new MyViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }

        String strName = mNameList.get(position);
        if (strName != null )
            viewHolder.attrNameText.setText(strName);

        String strValue = mValueList.get(position);
        if(strValue != null){
            viewHolder.attrValueText.setText(strValue);
        }
        return convertView;
    }

    static class MyViewHolder /*extends RecyclerView.ViewHolder */{
        @BindView(R.id.attr_name_text)
        TextView attrNameText;
        @BindView(R.id.attr_value_text)
        TextView attrValueText;

        public MyViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
