package com.wasu.cs.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.cs.aidl.IWasuInfo;
import com.wasu.cs.aidl.IWasuInfoListener;
import com.wasu.module.log.WLog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Danxingxi on 2016/7/22.
 * 为第三方apk提供设备和用户信息的service
 */
public class WasuInfoAIDLService extends Service {
    private static final String TAG = "WasuInfoAIDLService";
    private IWasuInfoListener infoCallBack;

    /**
     * 绑定service
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /**
     * 启动service
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        WLog.i(TAG,"onStartCommand()");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 解绑service
     * @param intent
     * @return
     */
    @Override
    public boolean onUnbind(Intent intent) {
        WLog.i(TAG,"onStartCommand()");
        infoCallBack = null;
        return super.onUnbind(intent);
    }

    IWasuInfo.Stub mBinder = new IWasuInfo.Stub(){
        /**
         *
         * @param extra
         * @param callback
         * @throws RemoteException
         */
        @Override
        public void getDeviceinfo(String extra, IWasuInfoListener callback) throws RemoteException {
            if(infoCallBack == null){
                if(callback == null){
                    WLog.e(TAG,"第三方APK获取华数信息的回调接口不能为null");
                    return;
                }else{
                    infoCallBack = callback;
                }
            }

            JSONObject jsonObject = new JSONObject();
            String infoStr = "";
            try {

                jsonObject.put("tvId", AuthSDK.getInstance().getValue(IAuthInterface.KEY_TVID));
                jsonObject.put("deviceId",AuthSDK.getInstance().getValue(IAuthInterface.KEY_DEVICEID));
                jsonObject.put("publicKey",AuthSDK.getInstance().getValue(IAuthInterface.KEY_PUBLICKEY));
                jsonObject.put("encryptV",AuthSDK.getInstance().getValue(IAuthInterface.KEY_ENCRYPTV));
                jsonObject.put("mac",AuthSDK.getInstance().getValue(IAuthInterface.KEY_MAC));
                jsonObject.put("siteId",AuthSDK.getInstance().getValue(IAuthInterface.KEY_SITEID));
                infoStr = jsonObject.toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if(!TextUtils.isEmpty(infoStr)){
                callback.onResultDeviceinfo(infoStr);
            }

        }

        /**
         *
         * @param extra
         * @param callback
         * @throws RemoteException
         */
        @Override
        public void getUserinfo(String extra, IWasuInfoListener callback) throws RemoteException {
            if(infoCallBack == null){
                if(callback == null){
                    WLog.e(TAG,"第三方APK获取华数信息的回调接口不能为null");
                    return;
                }else{
                    infoCallBack = callback;
                }
            }
            JSONObject jsonObject = new JSONObject();
            String infoStr = "";
            try {
                jsonObject.put("phone",AuthSDK.getInstance().getValue(IAuthInterface.KEY_PHONE));
                jsonObject.put("token",AuthSDK.getInstance().getValue(IAuthInterface.KEY_TOKEN));
                jsonObject.put("userKey",AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY));
                jsonObject.put("headUrl","headUrl");
                jsonObject.put("loginType","loginType");
                jsonObject.put("expand","expand");
                infoStr = jsonObject.toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if(!TextUtils.isEmpty(infoStr)){
                infoCallBack.onResultUserinfo(infoStr);
            }
        }
    };
}
