package ai.pai.lensman.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.android.common.executors.ThreadExecutor;

import java.util.HashMap;

import ai.pai.lensman.bean.OrderBean;
import ai.pai.lensman.db.DBService;
import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.utils.HttpPostTask;

public class OrderDealService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        return new OrderServiceBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startQuery();
        return START_STICKY;
    }

    private void startQuery(){
        HashMap<String,String> params = new HashMap<>();
        params.put("user_id", Preferences.getInstance().getLensManId());
        new HttpPostTask(params){
            OrderBean orderBean;
            @Override
            protected boolean parseResponse(String response) {
                orderBean = new OrderBean();
                //TODO
                return super.parseResponse(response);
            }

            @Override
            protected void onPostSuccess() {
                super.onPostSuccess();
                onNewOrderArrived(orderBean);
            }

            @Override
            protected void onPostFail() {
                super.onPostFail();
            }
        }.executeOnExecutor(ThreadExecutor.getInstance().getExecutor());

    }

    private void onNewOrderArrived(OrderBean bean){
        DBService.getInstance().addOrderBean(bean);
    }

    public class OrderServiceBinder extends Binder {

        public OrderDealService getService(){
            return OrderDealService.this;
        }

    }

}
