package com.itant.joycity.adapter;

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

import com.itant.joycity.R;
import com.itant.joycity.bean.UserInfo;
import com.itant.joycity.tool.ToastTool;

import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 * 图片列表适配器
 */
public class UserInfoAdapter extends BaseAdapter {

    private List<UserInfo> userInfos;
    private Context context;
    private ClipboardManager cm;


    public UserInfoAdapter() {

    }

    public UserInfoAdapter(Context context, List<UserInfo> userInfos) {
        this.userInfos = userInfos;
        this.context = context;

        cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return userInfos == null ? null : userInfos.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final UserInfo userInfo = userInfos.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_bank, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_bank_account = (TextView) convertView.findViewById(R.id.tv_bank_account);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_bank = (TextView) convertView.findViewById(R.id.tv_bank);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_bank_account.setText(userInfo.getCardNo());
        viewHolder.tv_bank_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 银行账号
                cm.setText(userInfo.getCardNo());
                ToastTool.showShort(context, "复制银行账号成功");
            }
        });

        viewHolder.tv_name.setText(userInfo.getName());
        viewHolder.tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 姓名
                cm.setText(userInfo.getName());
                ToastTool.showShort(context, "复制姓名成功");
            }
        });

        viewHolder.tv_bank.setText(userInfo.getBankName());
        viewHolder.tv_bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开户银行
                cm.setText(userInfo.getBankName());
                ToastTool.showShort(context, "复制开户银行成功");
            }
        });

        return convertView;
    }

    private ViewHolder viewHolder;
    private static class ViewHolder {
        TextView tv_bank_account;
        TextView tv_name;
        TextView tv_bank;
    }
}
