package w.soulofpluto.posstock;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
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{
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    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("Content-Type", "application/json")
                .addHeader("Pos-Access-Token",preferenceManager.getCustToken())
                .addHeader("Pos-User-Access-Store-Id", 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(new Runnable() {
                        @Override
                        public void run() {
                            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);
        //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("Content-Type", "application/json")
                .addHeader("Pos-Access-Token",preferenceManager.getCustToken())
                .addHeader("Pos-User-Access-Store-Id", 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(new Runnable() {
                        @Override
                        public void run() {
                            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("Content-Type", "application/json")
                .addHeader(context.getResources().getString(R.string.accesstoken),preferenceManager.getCustToken())
                .addHeader(context.getResources().getString(R.string.storeidtoken),preferenceManager.getStoreId())
                .build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Request request, IOException e){
                //featureDataInterface.FeatureFailure(request.toString());
                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("Content-Type", "application/json")
                .addHeader(context.getResources().getString(R.string.accesstoken),preferenceManager.getCustToken())
                .addHeader(context.getResources().getString(R.string.storeidtoken),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(new Runnable(){
                    @Override
                    public void run(){
                    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);
        System.out.println("Url    ====>"+Url);
        System.out.println("Object ====>"+object);
        System.out.println("Token  ====>"+preferenceManager.getCustToken());
        final Request request = new Request.Builder()
                .url(Url)
                .post(object)
                .addHeader("Content-Type", "application/json")
                .addHeader("Pos-Access-Token",  preferenceManager.getCustToken())
                .addHeader("Pos-User-Access-Store-Id", 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();
                    System.out.println("Customer Response ====>" + Response);
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            dataInterface.onCustomerName(response.code(), Response);
                        }
                    });
            }
        });
    }
}
