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

import android.content.Context;
import android.text.TextUtils;
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.module.domain.AttributeBean;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.FieldsBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class PipenetCalloutViewAdapter extends BaseAdapter {
    private Map<String, Object> mAttrsMap;//管线属性
    //    private List<Field> mFieldList;//字段名
//    private List<FeatureMetaDataBean> mMetaDataList;
    private List<?> filedCfgs;
    private Context mContext;
    private String tableName;
    private List<AttributeBean> mAttributes = new ArrayList<>();

    private static HashMap<String, String > fieldMap = new HashMap<String, String>(){{
        put("高压管线,次高压管线,中压管线,低压管线,预埋管线","管径,类型,平均埋深,材质,运行压力,壁厚,接口方式,设计压力,防腐材料,腐蚀状况,施工单位,竣工日期,维修日期,录入日期,修改日期,维护次数,数据来源,安全评级,状态,埋设方式,隐患情况,巡检人,审核人,采集人,录入人,阴极保护类型,阴极保护测试点数,长度,备注1");
        put("调压设备", "设备编码,类型,安装地址,运行压力,设计压力,开闭状态,生产厂家,型号,进口压力,切断压力,关闭压力,竣工日期,录入日期,修改日期,维护次数,维修日期,检修次数,检修日期,巡检次数,巡检日期,数据来源,防腐等级,状态,隐患情况,巡检人,审查人,采集人,录入人,X坐标,Y坐标");
        put("阀门", "设备编码,类型,管径,生产厂家,材质,埋深,阀门型号,竣工日期,维修日期,录入日期,修改日期,维护次数,数据来源,防腐等级,状态,埋设方式,隐患情况,巡检人,审查人,采集人,录入人,X坐标,Y坐标,备注1,阀门性质");
        put("SCADA监测点", "设备编码,类型,型号,生产厂家,流量,压力变送器型号及编号1,压力2,压力变送器型号及编号2,压力变送器型号及编号3,压力1,数据来源,状态,X坐标,Y坐标,建设时间,点位类型,站点名称,备注1");
        put("阴极保护", "设备编码,类型,工程名称,管径,材质,极性材质,开路电压,保护电压,保护电流,杂散电流情况,检测片情况,设计压力,保护措施,运行压力,保护起止点,竣工日期,维修日期,录入日期,修改日期,维护次数,数据来源,防腐等级,状态,埋设方式,隐患情况,巡检人,审查人,采集人,录入人,X坐标,Y坐标,备注1");
        put("套管", "自编号,类型,管径,材质,壁厚,埋深,管顶高程,运行压力,保护措施,防腐情况,竣工日期,维修日期,录入日期,修改日期,工程性质,维护次数,数据来源,防腐等级,状态,埋设方式,隐患情况,巡检人,审查人,采集人,录入人,X坐标,Y坐标,备注1");
        put("节点", "编号,类型,管径,弯管角度,材质,埋深,管顶高程,运行压力,设计压力,竣工日期,维修日期,录入日期,修改日期,维护次数,数据来源,防腐等级,状态,埋设方式,隐患情况,巡检人,审查人,采集人,录入人,X坐标,Y坐标,施工单位,生产厂家,防腐材料,防腐材料厂家,防腐方式,备注1");
        put("场站", "名称,地址,供气能力,设计压力,工作压力,设备登记,气源");
        put("燃气桩", "自编号,X坐标,Y坐标,材质");
        put("气源", "分类,名称,供气压力,最大供气量,最大瞬量");
        put("穿越点", "自编号,投用时间,状态,穿跨越物,材质,穿跨方式,涵洞参数");
    }};

    public PipenetCalloutViewAdapter(Context context, Map<String, Object> attrs, List<?> allConfigs, String tableName, boolean bPairAlias,  boolean bUseAliasName) {
        mContext = context;
        mAttrsMap = attrs;
        this.tableName = tableName;
        this.filedCfgs = allConfigs;
        initDataWithAllConfig(bPairAlias, bUseAliasName);
    }

    /**
     * @param bPairAlias 匹配的时候是否使用别名(中文名)
     * @param bAliasName 显示时候是否使用别名(中文名)
     */
    private void initDataWithAllConfig(boolean bPairAlias, boolean bAliasName) {
        if (mAttrsMap != null && mAttrsMap.size() > 0) {
            if (filedCfgs != null && filedCfgs.size() > 0) {
                //String localConfigs = getLocalConfig();
                List<AttributeBean> unIndexList = new ArrayList<>();
                for (Object obj : filedCfgs) {
                    String fieldName = "";
                    String aliasName = "";
                    if (obj instanceof Field) {
                        Field field = (Field) obj;
                        fieldName = field.getName();
                        aliasName = field.getAlias();
                    } else if (obj instanceof FieldsBean) {
                        FieldsBean fieldsBean = (FieldsBean) obj;
                        fieldName = fieldsBean.getValue();
                        aliasName = fieldsBean.getNotes();
                    }

                    String pairName = bPairAlias ? aliasName : fieldName;
                    String useName = bAliasName ? aliasName : fieldName;
                    if (fieldName == null) {
                        pairName = aliasName;
                        useName = aliasName;
                    }
                    String strValue = "";
                    if (mAttrsMap.containsKey(pairName)) {
                        Object value = null;
                        /*if (localConfigs != null) {
                            if (localConfigs.contains(useName)) {
                                value = mAttrsMap.get(pairName);
                            }
                        }else*/ {
                            value = mAttrsMap.get(pairName);
                        }

                        if (value != null) {
                            if (value instanceof GregorianCalendar) {
                                strValue = DateUtils.gregorianCalendarToString((GregorianCalendar) value);
                            }else {
                                strValue = value.toString();
                                if (strValue.compareToIgnoreCase("null") == 0){
                                    strValue = "";
                                }
                            }
                            AttributeBean attributeBean = new AttributeBean(useName, strValue);
                            mAttributes.add(attributeBean);
                        }
                    } else {
                        /*AttributeBean attributeBean = new AttributeBean(aliasName, strValue);
                        unIndexList.add(attributeBean);*/
                    }
                }
                if (unIndexList.size() > 0){
                    mAttributes.addAll(unIndexList);
                }
            }else {
                initWithNoConfig();
            }
        }
    }

    private String getLocalConfig() {
        String findValue = null;
        if (tableName != null && !tableName.isEmpty()) {
            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                String key = entry.getKey();
                if (key.contains(tableName)) {
                    findValue = entry.getValue();
                    break;
                }
            }
        }
        return findValue;
    }

    private void initWithNoConfig() {
        if (mAttrsMap != null && mAttrsMap.size() > 0){
            for (Map.Entry<String, Object> entry : mAttrsMap.entrySet()){
                String aliasName = entry.getKey();
                Object object = entry.getValue();
                String strValue = "";
                if (object != null) {
                    strValue = object.toString();
                }
                AttributeBean attributeBean = new AttributeBean(aliasName, strValue);
                mAttributes.add(attributeBean);
            }
        }
    }


//    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) {
//                        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;
//    }

//    private void initDataWithMedaData() {
//        if (mAttrsMap != null && mAttrsMap.size() > 0) {
//            for (FeatureMetaDataBean metaData : mMetaDataList) {
//                String fieldname = metaData.getS_FIELDNAME();
//                if (mAttrsMap.containsKey(fieldname)) {
//                    String aliasName = metaData.getS_FIELDALIASNAME();
//                    mNameList.add(aliasName);
//                    Object value = mAttrsMap.get(fieldname);
//                    String strValue = "";
//                    if (value != null) {
//                        strValue = value.toString();
//                    }
//                    mValueList.add(strValue);
//                }
//            }
//        }
//    }

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

    @Override
    public Object getItem(int position) {
        return mAttributes.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_callout_pipenet, null);
            viewHolder = new ViewHolder();
            viewHolder.attrNameText = convertView.findViewById(R.id.attr_name_text);
            viewHolder.attrValueText = convertView.findViewById(R.id.attr_value_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //int count = getCount();

        AttributeBean attribute =  (AttributeBean) getItem(position);
        String strName = attribute.getKey();
        String strValue = attribute.getValue();

        if (!TextUtils.isEmpty(strName)) {
            viewHolder.attrNameText.setText(strName);
        }else {
            viewHolder.attrNameText.setText("");
        }

        if (!TextUtils.isEmpty(strValue)) {
            viewHolder.attrValueText.setText(strValue);
        }else {
            viewHolder.attrValueText.setText("");
        }
        return convertView;
    }

    private class ViewHolder {
        TextView attrNameText;
        TextView attrValueText;
    }
}
