package com.alipay.face.verify.fintechfacedemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.alibaba.fastjson.JSONObject;
import com.alipay.face.verify.fintechfacedemo.utils.Base64Util;
import com.alipay.face.verify.fintechfacedemo.utils.TextUtil;

import java.util.ArrayList;
import java.util.List;

public class ResultAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<ResultItem> resultItemList = new ArrayList<>();
    private final Context context;

    public ResultAdapter(Context context, JSONObject jsonResult) {
        this.context = context;
        for (String key : jsonResult.keySet()) {
            try {
                String value = jsonResult.getString(key);
                if (TextUtil.isNotEmpty(value)) {
                    ResultItem resultItem = new ResultItem(key, value);
                    resultItemList.add(resultItem);
                }
            } catch (Exception e) {
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_result_list, viewGroup, false);
        return new ResultViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        String key = resultItemList.get(position).key;
        String value = resultItemList.get(position).value;
        ((ResultViewHolder) viewHolder).txtKey.setText(translate(key));
        if (key.equals("picture") || key.equals("docImage") || key.equals("docImage2")) {
            Bitmap bitmapPicture = bytes2Bitmap(Base64Util.decodeBase64(value.getBytes()));
            ((ResultViewHolder) viewHolder).imageView.setImageBitmap(bitmapPicture);
        } else {
            ((ResultViewHolder) viewHolder).txtValue.setText(value);
        }
    }

    public static Bitmap bytes2Bitmap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    @Override
    public int getItemCount() {
        return resultItemList.size();
    }

    static class ResultViewHolder extends RecyclerView.ViewHolder {

        public final TextView txtKey;
        public final TextView txtValue;
        public final ImageView imageView;

        public ResultViewHolder(View itemView) {
            super(itemView);
            txtKey = itemView.findViewById(R.id.txt_key);
            txtValue = itemView.findViewById(R.id.txt_value);
            imageView = itemView.findViewById(R.id.image);
        }
    }

    public String translate(String en) {
        switch (en) {
            case "address":
                return "住址";
            case "nationality":
                return "国籍";
            case "idNum":
                return "证件号码";
            case "sex":
                return "性别";
            case "name":
                return "姓名";
            case "enName":
                return "英文名";
            case "nation":
                return "民族";
            case "BirthDate":
                return "生日";
            case "beginTime":
                return "有效其实日期";
            case "endTime":
                return "有效截止时间";
            case "countryCode":
                return "国家或地区码";
            case "machineReadCode":
                return "机读码";
            case "otherIdNum":
                return "通行证号码";
            case "signingTimes":
                return "签发次数";
            case "otherName":
                return "身份证繁体姓名";
            case "signingOrganization":
                return "签发机关";
            case "picture":
                return "高清人像";
            case "docImage":
            case "docImage2":
                return "合成证件图";
        }
        return en;
    }
}
