package w.soulofpluto.posstock;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

  /*
   Create by Ramesh Babu
  */

public class DataPresenter{
    private Context           context;
    private DataInterface     dataInterface;
    private String            Response;
    private PreferenceManager preferenceManager;

    public DataPresenter(Context context,DataInterface dataInterface){
        this   .context       =  context;
        this   .dataInterface =  dataInterface;
        preferenceManager     =  new PreferenceManager(context);
    }

    public void SendDeliverySlipNumber(String Url, RequestBody object, final String Id){
        System .out.println("Url ====>"+Url);
        System .out.println("Object ====>"+object);
        //RequestBody body = RequestBody.create(JSON, object);
        final OkHttpClient client = new OkHttpClient();
        client .setConnectTimeout(240,       TimeUnit.SECONDS);
        client .setReadTimeout(240,          TimeUnit.SECONDS);
        client .setWriteTimeout(240,         TimeUnit.SECONDS);
        Request request = new Request.Builder()
               .url(Url)
               .post(object)
               .addHeader(Constants.Type,           Constants.App_json)
               .addHeader(Constants.Access_Token,   preferenceManager.getCustToken())
               .addHeader(Constants.Access_StoreId, preferenceManager.getStoreId())
               .build();
        client .newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
                //featureDataInterface.FeatureFailure(request.toString());
               dataInterface.onDeliveryFail(request.toString());
               System.out.println("Delivery Slip error" +e.getMessage());
            }
            @Override
            public void onResponse(final com.squareup.okhttp.Response response) throws IOException{
               try {
                   Response = response.body().string();
                   System.out.println("Delivery Slip" + Response);
                   Handler handler = new Handler(Looper.getMainLooper());
                   handler.post(() -> dataInterface.onDeliveryData(response.code(),Response,Id));
               }catch (NullPointerException np){
                   np.printStackTrace();
               }finally {
                   response.body().close();
               }
            }
        });
    }


    public void SendDeliverySlipNumberRemove(String Url, RequestBody object){
        //System.out.println("Url ====>"+Url);
        //System.out.println("Object ====>"+object);
        final OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(240, TimeUnit.SECONDS);
        client.setReadTimeout(240,TimeUnit.SECONDS);
        client.setWriteTimeout(240,TimeUnit.SECONDS);
        Request request = new Request.Builder()
                .url(Url)
                .post(object)
                .addHeader(Constants.Type,           Constants.App_json)
                .addHeader(Constants.Access_Token,   preferenceManager.getCustToken())
                .addHeader(Constants.Access_StoreId, preferenceManager.getStoreId())
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
                //featureDataInterface.FeatureFailure(request.toString());
                dataInterface.onDeliveryFailRemove(request.toString());
                System.out.println("Delivery Slip remove error" +e.getMessage());
            }
            @Override
            public void onResponse(final com.squareup.okhttp.Response response) throws IOException{
                try {
                    Response = response.body().string();
                    System.out.println("Delivery Slip Remove" + Response);
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(() -> dataInterface.onDeliveryDataRemove(response.code(),Response));
                }catch (NullPointerException np){
                    np.printStackTrace();
                }finally {
                    response.body().close();
                }
            }
        });
    }


    public void FinalData(String Url, RequestBody object){
        System.out.println("Url ====>"+Url);
        System.out.println("Object ====>"+object);
        //RequestBody body = RequestBody.create(JSON, object);
        final OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(240, TimeUnit.SECONDS);
        client.setReadTimeout(240,TimeUnit.SECONDS);
        client.setWriteTimeout(240,TimeUnit.SECONDS);
        Request request = new Request.Builder()
                .url(Url)
                .post(object)
                .addHeader(Constants.Type, Constants.App_json)
                .addHeader(Constants.Access_Token,preferenceManager.getCustToken())
                .addHeader(Constants.Access_StoreId, preferenceManager.getStoreId())
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
                dataInterface.onFinalFail(request.toString());
                System.out.println("singup1 error" +e.getMessage());
            }
            @Override
            public void onResponse(final com.squareup.okhttp.Response response) throws IOException{
                try {
                    Response = response.body().string();
                    System.out.println("Signup1 Response" + Response);
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(() -> dataInterface.onFinalData(response.code(),Response));
                }catch (NullPointerException np){
                    np.printStackTrace();
                }finally {
                    response.body().close();
                }
            }
        });
    }

    public void Logout(String Url){
        final OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(240, TimeUnit.SECONDS);
        client.setReadTimeout(240,TimeUnit.SECONDS);
        client.setWriteTimeout(240,TimeUnit.SECONDS);
        Request request = new Request.Builder()
                .url(Url)
                .get()
                .addHeader(Constants.Type, Constants.App_json)
                .addHeader(Constants.Access_Token,preferenceManager.getCustToken())
                .addHeader(Constants.Access_StoreId, preferenceManager.getStoreId())
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
                dataInterface.onLogoutFail(request.toString());
                System.out.println("cart count error ====>" +e.getMessage());
            }
            @Override
            public void onResponse(final com.squareup.okhttp.Response response) throws IOException{
                Response = response.body().string();
                System.out.println("cart count Response ====>" +Response);
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(() -> dataInterface.onLogoutData(response.code(),Response));
            }
        });
    }

    public void CustomerName(String Url,RequestBody object){
        final OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(2000, TimeUnit.SECONDS);
        client.setReadTimeout(2000,    TimeUnit.SECONDS);
        client.setWriteTimeout(2000,   TimeUnit.SECONDS);
        final Request request = new Request.Builder()
                .url(Url)
                .post(object)
                .addHeader(Constants.Type,           Constants.App_json)
                .addHeader(Constants.Access_Token,   preferenceManager.getCustToken())
                .addHeader(Constants.Access_StoreId, preferenceManager.getStoreId())
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
                dataInterface.onCustomerNameFail(request.toString());
                System.out.println("Customer error ====>" +e.getMessage());
            }
            @Override
            public void onResponse(final com.squareup.okhttp.Response response) throws IOException{
                Response = response.body().string();
                Log.e("Customer Response ====>" , Response);
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(() -> dataInterface.onCustomerName(response.code(), Response));
            }
        });
    }
}
