package com.miya.weighing;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.miya.weighing.utils.D;
import com.sunmi.electronicscaleservice.ScaleCallback;
import com.sunmi.electronicscaleservice.ScaleService;

/**
 * 商米称重硬件驱动(注意：无需使用jar包中的ScaleManager!!!)
 *
 * @author pupu
 * @time 2018/10/26
 */
public class SunmiWeighing extends BaseWeighing {

    final static String TAG = SunmiWeighing.class.getSimpleName();

    final static String SERVICE_PACKAGE = "com.sunmi.electronicscaleservice";

    final static String SERVICE_ACTION = "com.sunmi.scaleservice";

    /**
     * 称重服务
     */
    ScaleService scaleService;

    /**
     * 称重回调监听
     */
    ScaleCallback.Stub stub = new ScaleCallback.Stub() {
        @Override
        public void getData(int net, int tare, int status) throws RemoteException {
            D.i(TAG, "商米称重硬件==>原始重量：" + net + ";皮重：" + tare + ";状态：" + status);
            if (weighingCallback != null) {
                String stateStr = Integer.toBinaryString(status);
                boolean isStable;
                if (stateStr.substring(stateStr.length() - 1).equals("1")) {
                    // 稳定
                    isStable = true;
                    if (net < 0) {
                        //程序置零一次,可能会失败
                        resetZero();
                    }
                } else {
                    // 动态值
                    isStable = false;
                }
                curWeight = net;
                weighingCallback.returnWeight(net, isStable);
            }
        }
    };

    @Override
    public boolean init(Context context) {
        this.context = context;
        Intent intent = new Intent();
        intent.setPackage(SERVICE_PACKAGE);
        intent.setAction(SERVICE_ACTION);
        return this.context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void setWeighingCallback(WeighingCallback weighingCallback) {
        this.weighingCallback = weighingCallback;
    }

    @Override
    public boolean resetZero() {
        try {
            scaleService.zero();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 服务连接器
     */
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            scaleService = ScaleService.Stub.asInterface(service);
            if (scaleService != null) {
                //设置重量回调
                try {
                    scaleService.getData(stub);
                } catch (Exception e) {
                    e.printStackTrace();
                    D.i(TAG, "称重服务初始化失败!");
                }
            } else {
                D.i(TAG, "防损称连接失败!");
            }
        }

        @Override
        public void onBindingDied(ComponentName name) {
            D.i(TAG, "解绑成功 ComponentName==>" + name);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            scaleService = null;
        }
    };

    @Override
    public boolean release() {
        if (context != null && serviceConnection != null) {
            this.context.unbindService(serviceConnection);
        }
        context = null;
        serviceConnection = null;
        if (scaleService != null) {
            try {
                scaleService.cancelGetData();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        scaleService = null;
        this.weighingCallback = null;
        return true;
    }
}
