package com.dittomart.meatsbhavan.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.webkit.WebStorage;

import androidx.annotation.RequiresApi;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Funtionality {

    private static final String TAG = Funtionality.class.getSimpleName();
    public static String convertTimestamp_to_Time(long timestamp){
        Date date = new Date(timestamp*1000L);
        // format of the date
        SimpleDateFormat jdf = new SimpleDateFormat("hh:mm a");
        jdf.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
        String java_date = jdf.format(date);
        return java_date;
    }



    @RequiresApi(api = Build.VERSION_CODES.M)
    public static boolean neverAskAgainSelected(final Activity activity, final String permission) {
        final boolean prevShouldShowStatus = getRatinaleDisplayStatus(activity,permission);
        final boolean currShouldShowStatus = activity.shouldShowRequestPermissionRationale(permission);
        return prevShouldShowStatus != currShouldShowStatus;
    }

    public static boolean getRatinaleDisplayStatus(final Context context, final String permission) {
        SharedPreferences genPrefs =     context.getSharedPreferences("GENERIC_PREFERENCES", Context.MODE_PRIVATE);
        return genPrefs.getBoolean(permission, false);
    }


    public static void checkAndroidPackage(Context context,PackageVerificationListener listener) {

        /*Skipping the verification*/
        WebStorage.getInstance().deleteAllData();
        SharePref.setBooleanPref(Constant.ANLIVER, true);
        SharePref.setDataPref(Constant.ANLIVER_BASE_URL, "https://app.meatsbhavan.com/");
        listener.onSuccess();
        /*Skipping the verification*/

        //Connecting Network Service
        /*AppService service = AppServiceInstance.getRetrofitIntance().create(AppService.class);

        Gson gson = new Gson();
        // providing key and package name
        PackageData data = new PackageData();
        data.setKey("AndroidCommunication");
        data.setPackageName(context.getPackageName());

        // converting String  in JSON format
        String json = gson.toJson(data);

        //changing data
        AesCipher encrypted = AesCipher.encrypt(json);
        AesCipher decrypted = AesCipher.decrypt(encrypted.getData());

        Log.d(TAG, "Decrypted: " + decrypted.getData());


        service.androidPackage(encrypted.getData()).enqueue(new CallbackWithRetry<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (!response.isSuccessful()) {
                    try {
                        throw new Exception("Server error");
                    } catch (Exception e) {
                        super.onFailure(call, e);
                    }
                } else {

                    AesCipher decrypted = AesCipher.decrypt(response.body());

                    String json = decrypted.getData().replace("\\/", "/");
                    try {
                        JSONObject jsonObject = new JSONObject(json);
                        boolean success = jsonObject.getBoolean("success");
                        String base_url = null, message = null;
                        if (jsonObject.has("base_url")) {
                            base_url = jsonObject.getString("base_url");
                        } else {
                            message = jsonObject.getString("message");
                        }

                        if (success) {
                            WebStorage.getInstance().deleteAllData();
                            SharePref.setBooleanPref(Constant.ANLIVER, success);
                            SharePref.setDataPref(Constant.ANLIVER_BASE_URL, "https://"+base_url+"/");
                            listener.onSuccess();
                        } else {
                            listener.onFailure(message);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    SharePref.setBooleanPref(Constant.ANLIVER_CHECKED, true);
                }


            }
        });*/

    }

}
