package chaoran.business.adapter;


import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;

import java.util.List;

import chaoran.business.activity.ResultListener;
import chaoran.business.service.ScanServiceEDA50P;
import chaoran.business.service.ScanServiceZEBRA;
import chaoran.business.strategy.Strategy;


public class HoneywellAdapter implements Adapter {
    private Context context;
    private Strategy strategy;
    private ResultListener resultListener;
    private Intent intent = null;

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

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

    @Override
    public void stop2() {

    }

    @Override
    public void stop() {
        new ScanServiceEDA50P().onDestroy();
        strategy.exclusiveStrategy();
        this.stopContinueScan();
    }

    public class Receiver extends BroadcastReceiver implements Strategy {

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

        @Override
        public void executeStrategy(ResultListener resultListener) {
            IntentFilter filter = new IntentFilter();
            filter.addAction("com.honeywell.scan.broadcast");
            context.registerReceiver(this, filter);
        }

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

    public void stopContinueScan() {
        Class clazz = null;
        if("eda50p".equals(Build.MODEL.toLowerCase())){ // 扫描正常
            intent = new Intent(context, ScanServiceEDA50P.class);
            clazz = ScanServiceEDA50P.class;
        }else if("eda51".equals(Build.MODEL.toLowerCase())){ // 扫描正常
            intent = new Intent(context, ScanServiceEDA50P.class);
            clazz = ScanServiceEDA50P.class;
        }
        else if("tc26".equals(Build.MODEL.toLowerCase())){
            intent = new Intent(context, ScanServiceZEBRA.class);
            clazz = ScanServiceZEBRA.class;
        }
        if (clazz != null && isServiceRunning(clazz)) {
            context.stopService(intent);
        }
    }

    private boolean isServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> runningServices = manager.getRunningServices(Integer.MAX_VALUE);

        for (ActivityManager.RunningServiceInfo service : runningServices) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    public void openContinueScan(){

        if("eda50p".equals(Build.MODEL.toLowerCase())){ // 扫描正常
            intent = new Intent(context, ScanServiceEDA50P.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.getApplicationContext().startForegroundService(intent);
            }else {
                context.getApplicationContext().startService(intent);
            }
        }else if("eda51".equals(Build.MODEL.toLowerCase())){ // 扫描正常
            intent = new Intent(context, ScanServiceEDA50P.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.getApplicationContext().startForegroundService(intent);
            }else {
                context.getApplicationContext().startService(intent);
            }
        }
        else if("tc26".equals(Build.MODEL.toLowerCase())){
            intent = new Intent(context, ScanServiceZEBRA.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.getApplicationContext().startForegroundService(intent);
            }else {
                context.getApplicationContext().startService(intent);
            }
        }
    }
}
