package com.yijia.zthandpos.pos.test;

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.szzt.android.util.HexDump;
import com.szzt.sdk.device.Constants.Error;
import com.szzt.sdk.device.card.SmartCardReader;
import com.yijia.zthandpos.pos.BaseActivity;
import com.yijia.zthandpos.pos.utils.MessageManager;
import com.yijia.zthandpos.pos.utils.Shower;
import com.yijia.zthandpos.pos.utils.StringUtility;
import com.yijia.zthandpos.pos.R;

public class ICCardActivity extends BaseActivity{
    private SmartCardReader mSmartCardReader;
    private Button mOpenButton;
    private Button mClearButton;
    private Button mCloseButton;
    private TextView mTextViewResult;
    MessageManager messageManager;
    private Shower shower ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);
        shower = new Shower(this) ;
        boolean isConnected = mainApplication.isDeviceManagerConnetcted();
        init(isConnected);
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	if (mSmartCardReader != null){
    		mSmartCardReader.close(0);
    	}
    }
    
    private void init(boolean isConnected) {
        if (mCloseButton == null) {
            mTextViewResult = (TextView) findViewById(R.id.mag_text);
            mTextViewResult.setMovementMethod(ScrollingMovementMethod
                    .getInstance());

            messageManager = new MessageManager(this, mTextViewResult);

            mOpenButton = (Button) findViewById(R.id.mag_open);
            mOpenButton.setOnClickListener(listener);

            mClearButton = (Button) findViewById(R.id.mag_clear);
            mClearButton.setOnClickListener(listener);


            mCloseButton = (Button) findViewById(R.id.mag_close);
            mCloseButton.setOnClickListener(listener);
        }
        mTextViewResult.setEnabled(isConnected);
        mOpenButton.setEnabled(isConnected);
        mClearButton.setEnabled(isConnected);
        mCloseButton.setEnabled(isConnected);
        if (isConnected) {
                mSmartCardReader = mainApplication.getSmartCardReader();
        } else {
            mSmartCardReader = null;
        }
    }
    
    void testSmartCardReader() {
        int result = mSmartCardReader.waitForCard(0, 60000);
        if (result == 0) {
        	byte[] data=new byte[256];
        	int type = mSmartCardReader.getCardType(0);
        	System.out.println("type:" + type);
        	if (type == SmartCardReader.CONTACT_CARD_TYPE_A_CPU){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                testSmartCardReaderIO();
        	}else if (type == SmartCardReader.CONTACT_CARD_TYPE_4442){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                messageManager.AppendInfoMessageInUiThread("type:4442");
                testMemoryCard();
        	}else if (type == SmartCardReader.CONTACT_CARD_TYPE_4428){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                messageManager.AppendInfoMessageInUiThread("type:4428");
                testMemoryCard();
        	}else if (type == SmartCardReader.CONTACT_CARD_TYPE_AT24C02){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                messageManager.AppendInfoMessageInUiThread("type:AT24C02");
                //TODO 
        	}else if (type == SmartCardReader.CONTACT_CARD_TYPE_AT88SC102){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                messageManager.AppendInfoMessageInUiThread("type:AT88SC102");
                //TODO
        	}else if (type == SmartCardReader.CONTACT_CARD_TYPE_AT88SC1604){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                messageManager.AppendInfoMessageInUiThread("type:AT88SC1604");
                //TODO
        	}else if (type == SmartCardReader.CONTACT_CARD_TYPE_AT88SC1608){
        		result=mSmartCardReader.powerOn(0, data);
                messageManager.AppendInfoMessageInUiThread("Power On ATR:" + (result>0?StringUtility.ByteArrayToString(data, result):""));
                messageManager.AppendInfoMessageInUiThread("type:AT88SC1608");
                //TODO
        	}else{
        		messageManager.AppendInfoMessageInUiThread("Not support");
        	}
        	
        } else if (result == Error.TIMEOUT) {
            messageManager.AppendInfoMessageInUiThread(shower.getString(R.string.msg_card_time_out),
                    Color.RED);
        } else if (result == Error.DEVICE_FORCE_CLOSED) {
            messageManager.AppendInfoMessageInUiThread(
                    shower.getString(R.string.msg_card_force_close), Color.RED);
        } else {
            messageManager.AppendInfoMessageInUiThread(
                    shower.getString(R.string.msg_card_read_error), Color.RED);
        }
    }
    
    void testMemoryCard(){
		//�����洢������������
		byte[] pvalue = new byte[32];
		int ret = mSmartCardReader.read(0, SmartCardReader.MemoryCardArea.MAIN, 0, pvalue);
		if (ret > 0){
			messageManager.AppendInfoMessageInUiThread("read main memory protected:"  + HexDump.toHexString(pvalue));
		}else{
			messageManager.AppendInfoMessageInUiThread("read main memory protected faild!");
		}
		
		//�����洢��
		byte[] data = new byte[16];
		ret = mSmartCardReader.read(0, SmartCardReader.MemoryCardArea.MAIN, 32, data);
		if (ret > 0){
			messageManager.AppendInfoMessageInUiThread("read main memory:"  + HexDump.toHexString(data, 0, ret));
		}else{
			 messageManager.AppendInfoMessageInUiThread("read main memory faild!");
		}
		//�������洢��
		byte[] pdata = new byte[8];
		ret = mSmartCardReader.read(0, SmartCardReader.MemoryCardArea.PROTECTED, 0, pdata);
		if (ret > 0){
			 messageManager.AppendInfoMessageInUiThread("read protected memory:"  + HexDump.toHexString(pdata, 0, ret));
		}else{
			 messageManager.AppendInfoMessageInUiThread("read protected memory faild!");
		}
		
		int count = getPassCount();
		if (count >= 0){
			 messageManager.AppendInfoMessageInUiThread("get pin check times:" + count);
		}else{
			 messageManager.AppendInfoMessageInUiThread("get pin check times fail��");
		}
		
		if (count == 1){
			//���һ��У�鲻������Ƭ�Ͳ�����У���ˡ� У��ʱ���м�ʹ����ȷ����У��
			messageManager.AppendInfoMessageInUiThread("the last check pin");
			return;
		}else if (count == 0){
			messageManager.AppendInfoMessageInUiThread("the check pin times is over");
		}
		
		//У������
		byte[] key = HexDump.hexStringToByteArray("FFFFFF");
		ret = mSmartCardReader.verify(0, key, SmartCardReader.KEY_TYPE_A);
		if (ret == 0){
			 messageManager.AppendInfoMessageInUiThread("verify succ!");
			
			writeData();
			
			writeProtectData();
			
		}else{
			 messageManager.AppendInfoMessageInUiThread("verify faild!");
			return;
		}
		
		ret = mSmartCardReader.read(0, SmartCardReader.MemoryCardArea.MAIN, 32, data);
		if (ret > 0){
			 messageManager.AppendInfoMessageInUiThread("read main memory:" +  HexDump.toHexString(data, 0, ret));
		}else{
			 messageManager.AppendInfoMessageInUiThread("read main memory faild!");
		}
		
		//�޸�����  ����Ϊ3���ֽ�
		byte[] pass = HexDump.hexStringToByteArray("FFFFFF");
		ret = mSmartCardReader.write(0, SmartCardReader.MemoryCardArea.SECURITY, 1, pass);
		if (ret >= 0){
			messageManager.AppendInfoMessageInUiThread("write pass succ!");
		}else{
			messageManager.AppendInfoMessageInUiThread("write pass faild!");
		}
    }
    
	
	/**
	 * ��ȡʣ������У�����
	 * @param data
	 * @return
	 */
	private int getPassCount() {
		int ret;
		byte[] count = new byte[1];
		ret = mSmartCardReader.read(0, SmartCardReader.MemoryCardArea.SECURITY, 0, count);
		if (ret > 0){
			System.out.println("read:" + HexDump.toHexString(count));
			if ((count[0] & 0x07) == 0x07){
				return 3;
			}else if ((count[0] & 0x03) == 0x03){
				return 2;
			}else if ((count[0] & 0x01) == 0x01){
				return 1;
			}else{
				return 0;
			}
		}else{
			messageManager.AppendInfoMessageInUiThread("read faild!");
		}
		return -1;
	}

	/**
	 * д���洢��
	 */
	private void writeData() {
		int ret;
		String msg = "hello:" + System.currentTimeMillis();
		//д���ݵ���������
		ret = mSmartCardReader.write(0, SmartCardReader.MemoryCardArea.MAIN, 32, msg.getBytes());
		if (ret >= 0){
			messageManager.AppendInfoMessageInUiThread("write succ!");
		}else{
			messageManager.AppendInfoMessageInUiThread("write faild!");
		}
	}

	/**
	 * �̻����������ݣ��ȶ�ȡ���룬Ȼ��д�룬д������ݾͲ����ٱ��޸�
	 */
	private void writeProtectData() {
		int ret;
		//д�̻�����������
		byte[] ppp = new byte[1];
		ret = mSmartCardReader.read(0, SmartCardReader.MemoryCardArea.MAIN, 5, ppp);
		if (ret > 0){
			messageManager.AppendInfoMessageInUiThread("�����洢��:"  + HexDump.toHexString(ppp));
		}else{
			messageManager.AppendInfoMessageInUiThread("�����洢��ʧ��!");
		}
		
		ret = mSmartCardReader.write(0, SmartCardReader.MemoryCardArea.PROTECTED, 5, ppp);
		if (ret >= 0){
			messageManager.AppendInfoMessageInUiThread("д�������ɹ�!");
		}else{
			messageManager.AppendInfoMessageInUiThread("д������ʧ��!");
		}
	}
    
    void testSmartCardReaderIO() {
        // 00 A4 04 00 0E 1PAY.SYS.DDF01
        byte[] apdu = new byte[] { (byte) 0x00, (byte) 0xA4, (byte) 0x04,
                (byte) 0x00, (byte) 0x0E, 0x31, 0x50, 0x41, 0x59, 0x2E, 0x53,
                0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31,0x00,0x00};
        byte[] ret = new byte[255];
        int len = mSmartCardReader.transmit(0, apdu, ret);
        if (len >= 0) {
            if (len >= 2 && ret[len - 2] == 97) {
                apdu = new byte[] { 0x00, (byte) 0xC0, 0x00, 0x00,
                        (byte) (ret[len - 1] & 0xFF) };
                ret = new byte[255];
                len = mSmartCardReader.transmit(0, apdu, ret);
            }
        }
        if (len >= 0) {
            messageManager.AppendInfoMessageInUiThread("APDU:"
                    + StringUtility.ByteArrayToString(ret, len));
        } else {
            messageManager.AppendInfoMessageInUiThread(
                    shower.getString(R.string.msg_card_exe_apdu_error), Color.RED);
        }
    }
    
    private final View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.mag_open: {
                	// if don't want to get card remove notice can using mSmartCardReader.open(0, null);
                    int nret = mSmartCardReader.open(0,
                                        new SmartCardReader.SCReaderListener(){
                                            @Override
                                            public void notify(
                                                    int nSlotIndex,
                                                    int nEvent) {
                                                if (nEvent == SmartCardReader.EVENT_SMARTCARD_NOT_READY) {
                                                    messageManager.AppendInfoMessageInUiThread(
                                                            shower.getString(R.string.msg_card_unplug_card), Color.RED);
                                                }else if (nEvent == SmartCardReader.EVENT_SMARTCARD_READY){
                                                	
                                                }
                                            }
                                        });
	                    if (nret >= 0) {
	                        messageManager
	                                .AppendInfoMessage(shower.getString(R.string.msg_card_open_success));
	                        messageManager
	                                .AppendInfoMessage(
	                                        shower.getString(R.string.msg_card_wait_for_card),
	                                        Color.GREEN);
	                        new Thread(){
	                        	@Override
	                        	public void run() {
	                        		testSmartCardReader();
	                        	}
                            }.start();
	                    } else {
	                        messageManager
	                                .AppendInfoMessage(shower.getString(R.string.msg_card_open_error));
	                    }
	                }
                	break;
                
                case R.id.mag_clear:
                    messageManager.clear();
                    break;
                    
                case R.id.mag_close: {
                    int ret = mSmartCardReader
                                .close();
                    if (ret >= 0) {
                        messageManager
                                .AppendInfoMessage(shower.getString(R.string.msg_card_close_succ));
                    } else {
                        messageManager
                                .AppendInfoMessage(
                                        shower.getString(R.string.msg_card_close_succ),
                                        Color.RED);
                    }
                }

                break;
                default:
                    break;
            }
        }
    };

}
