package com.synjones.idcardusb_reader;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.synjones.idcard.CountryCode;
import com.synjones.idcard.ForeignerIDCard;
import com.synjones.idcard.IDCards;

/**
 * Created by zhaodianbo on 2016-11-25.
 */
public class CardInfoView {
    private TextView textViewName;
    private TextView textViewSex;
    private TextView textViewNation;
    private TextView textViewBirthday;
    private TextView textViewAddress;
    private TextView textViewPIDNo;
    private TextView textViewGrantDept;
    private TextView textViewUserLife;
    private TextView tvFpAndAppendAdd;
    private ImageView imageViewPhoto ;
    Bitmap bmp;
    Activity mActivity;
    public CardInfoView(Activity activity){
        mActivity=activity;
        textViewName = (TextView) activity.findViewById(R.id.textViewName);
        textViewSex = (TextView) activity.findViewById(R.id.textViewSex);
        textViewNation = (TextView) activity.findViewById(R.id.textViewNation);
        textViewBirthday = (TextView) activity.findViewById(R.id.textViewBirthday);
        textViewAddress = (TextView) activity.findViewById(R.id.textViewAddress);
        textViewPIDNo = (TextView) activity.findViewById(R.id.textViewPIDNo);
        textViewGrantDept = (TextView) activity.findViewById(R.id.textViewGrantDept);
        textViewUserLife = (TextView) activity.findViewById(R.id.textViewUserLife);
        tvFpAndAppendAdd = (TextView) activity.findViewById(R.id.tvFpAndAppendAdd);
        imageViewPhoto = (ImageView) activity.findViewById(R.id.imageViewPhoto);
    }

    public void showCardView(IDCards idcard){
        if(idcard!=null){
            if(idcard.getCardType()== IDCards.CARDTYPE_NORMAL  || idcard.getCardType()== IDCards.CARDTYPE_HMT ) {//普通居民身份证 或 港澳台居住证
                textViewName.setText(mActivity.getString(R.string.sdtname) + idcard.getName());
                textViewSex.setText(mActivity.getString(R.string.sdtsex) + idcard.getSex());

                if(idcard.getCardType()== IDCards.CARDTYPE_HMT){
                    textViewNation.setText("通行证号:"+idcard.getPassID()+"\r\n签发次数:"+idcard.getIssuesTimes());
                }else{
                    textViewNation.setText(mActivity.getString(R.string.sdtnation) + idcard.getNation());
                }

                textViewBirthday.setText(mActivity.getString(R.string.sdtbirthday)
                        + idcard.getYearOfBirth() + "年"
                        + idcard.getMonthOfBirth() + "月"
                        + idcard.getDayOfBirth() + "日");
                textViewAddress.setText(mActivity.getString(R.string.sdtaddress) + idcard.getAddress());
                textViewPIDNo.setText(mActivity.getString(R.string.sdtpidno) + idcard.getIDCardNo());
                textViewGrantDept.setText(mActivity.getString(R.string.sdtgrantdept) + idcard.getGrantDept());
                textViewUserLife.setText(mActivity.getString(R.string.sdtuserlife) + idcard.getUserLifBebinWithPoint() + "-" + idcard.getUserLifEndWithPoint());
                tvFpAndAppendAdd.setVisibility(View.VISIBLE);
                tvFpAndAppendAdd.setText(mActivity.getString(R.string.append_msg) + idcard.getFpDescription());



            }else{//外国人居住证
                ForeignerIDCard card=idcard.getForeignerIDCard();
                textViewName.setText("姓名:"+card.getEngName()+"\r\n"+card.getCHNName());
                String sex=card.getSex();
                if(sex.equals("女")) sex+="/F";
                else sex+="/M";
                textViewSex.setText(mActivity.getString(R.string.sdtsex) +sex );
                textViewNation.setText("国籍:" + CountryCode.getContryByCode(card.getNation())+"/"+card.getNation());
                textViewBirthday.setText(mActivity.getString(R.string.sdtbirthday)
                        + card.getBirthdayWithSeparator("."));
                textViewAddress.setText("有效日期:"+ card.getUserLifBebinWithPoint()+"-"+card.getUserLifEndWithPoint());
                textViewPIDNo.setText("永久居留证号码:" +card.getIDCardNo());
                String grantDep= card.getGrantDep();
                if(grantDep.equals("1500"))
                    grantDep+="  公安部/Ministry of Public Security";
                textViewGrantDept.setText("签发机关代码:" +grantDep);
                textViewUserLife.setText("证件版本号:"+ card.getCertVol());
                tvFpAndAppendAdd.setText(idcard.getFpDescription());
            }
            tvFpAndAppendAdd.append("\r\n读卡时间："+idcard.getReadTime());
            try {
                //int result = idcard.decodeResult;
                bmp=idcard.getPhoto();
                if (bmp==null) {
                    Log.e(" ---------------",new Gson().toJson(mActivity.getResources()));
                    Resources res = mActivity.getResources();
                    bmp = BitmapFactory.decodeResource(res, R.drawable.photo);
                    //Log.e("状态：照片解码错");
                }
                imageViewPhoto.setImageBitmap(bmp);
                System.gc();

            } catch (Exception ioe) {
                ioe.printStackTrace();
            }

        }
        else{
            textViewName.setText(mActivity.getString(R.string.sdtname));
            textViewSex.setText(mActivity.getString(R.string.sdtsex));
            textViewNation.setText(mActivity.getString(R.string.sdtnation) );
            textViewBirthday.setText(mActivity.getString(R.string.sdtbirthday));
            textViewAddress.setText(mActivity.getString(R.string.sdtaddress));
            textViewPIDNo.setText(mActivity.getString(R.string.sdtpidno));
            textViewGrantDept.setText(mActivity.getString(R.string.sdtgrantdept) );
            textViewUserLife.setText(mActivity.getString(R.string.sdtuserlife) );
            tvFpAndAppendAdd.setText(mActivity.getString(R.string.append_msg));
            imageViewPhoto.setImageResource(R.drawable.photo);
        }
    }
}
