package com.stm.asset.adapter;

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

import com.stm.asset.R;
import com.stm.asset.model.AddressInfo;
import com.stm.asset.utils.StringUtils;

import java.util.List;

/**
 * 类名称：AddressAdapter
 * 类描述：地址适配器
 * 创建人：story
 * 创建时间：2022-01-06 09:52
 */
public class AddressAdapter extends BaseAdapter {

    private List<AddressInfo> dataList;
    private Context context;

    public AddressAdapter(Context context, List<AddressInfo> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList == null ? 0 : dataList.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_select_id_item, null);
            vh = new ViewHolder();
            vh.nameTxt = convertView.findViewById(R.id.select_ip_item_name);
            vh.ipTxt = convertView.findViewById(R.id.select_ip_item_ip);
            vh.platformTxt = convertView.findViewById(R.id.select_ip_item_platform);
            vh.selectImage = convertView.findViewById(R.id.select_ip_item_select_image);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        AddressInfo addressInfo = dataList.get(position);
        vh.nameTxt.setText(addressInfo.getName());
        // 处理ip
        StringBuffer sbf = new StringBuffer();
        if (addressInfo.isHttps()) {
            sbf.append("https://");
        } else {
            sbf.append("http://");
        }
        sbf.append(addressInfo.getIp());
        if (!StringUtils.isEmptyOrNull(addressInfo.getPort())) {
            sbf.append(":");
            sbf.append(addressInfo.getPort());
        }
        vh.ipTxt.setText(sbf.toString());

        vh.platformTxt.setText(addressInfo.getPlatform());
        return convertView;
    }

    private class ViewHolder {
        private TextView nameTxt, ipTxt, platformTxt;
        private ImageView selectImage;
    }

}
