package chaoran.business.adapter;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import chaoran.business.activity.ResultListener;
import chaoran.business.strategy.Strategy;
import com.android.barcodescandemo.ScannerInerface;

/**
 * idata 适配器
 */
public class IDataAdapter implements Adapter {
    private Context context;
    private ResultListener resultListener;
    private Strategy strategy;

    public IDataAdapter(Context context, ResultListener resultListener) {
        this.context = context;
        this.resultListener = resultListener;
        strategy = new Receiver();
    }

    @Override
    public void start() {
        strategy.executeStrategy(resultListener);
    }

    @Override
    public void stop() {
        strategy.exclusiveStrategy();
    }

    public class Receiver extends BroadcastReceiver implements Strategy {

        private ScannerInerface scannerInerface;

        public Receiver() {
            scannerInerface = new ScannerInerface(context);
//            scannerInerface.setOutputMode(1);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            resultListener.result(intent.getStringExtra("value"));
        }

        @Override
        public void executeStrategy(ResultListener resultListener) {
//            scannerInerface.open();
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.intent.action.SCANRESULT");
            context.registerReceiver(this, filter);
        }

        @Override
        public void exclusiveStrategy() {
            context.unregisterReceiver(this);
//            scannerInerface.close();
        }
    }
}
