package com.sferadev.danacast.utils;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;

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

import java.io.File;

public class UpdateUtils {
    private static final String URL = "https://raw.githubusercontent.com/SferaDev/DanaCast/master/updates/update.json";

    public static void checkUpdates(final Context context) {
        if (!getRemoteVersionCode().equals(getLocalVersionCode(context))) {
            final DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(getRemoteApkURL()));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                    File.separator + "DanaCast" + File.separator + "Updates" + File.separator + getRemoteVersionCode() + ".apk");
            request.setVisibleInDownloadsUi(false);
            final long id = downloadManager.enqueue(request);
            context.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    installApk(context, downloadManager.getUriForDownloadedFile(id));
                }
            }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        } else {

        }
    }

    private static String getRemoteVersionCode() {
        try {
            return new JSONObject(NetworkUtils.getURLOutput(URL)).getJSONObject("updateInfo").getString("versionCode");
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getLocalVersionCode(Context context) {
        try {
            return context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getRemoteApkURL() {
        try {
            return new JSONObject(NetworkUtils.getURLOutput(URL)).getJSONObject("updateInfo").getString("apkUrl");
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static void installApk(Context context, Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}

@@ -614,9 +614,8 @@
 if (
-!
 getR
@@ -636,16 +636,12 @@
 de()
-.equals(
+ != 
 getL
@@ -665,17 +665,16 @@
 ontext))
-)
  %7B%0A     
@@ -1613,38 +1613,35 @@
  private static 
-Str
 in
-g
+t
  getRemoteVersio
@@ -1675,32 +1675,49 @@
          return 
+Integer.parseInt(
 new JSONObject(N
@@ -1799,16 +1799,17 @@
 onCode%22)
+)
 ;%0A      
@@ -1882,36 +1882,34 @@
          return 
-null
+-1
 ;%0A        %7D%0A    
@@ -1926,30 +1926,27 @@
 vate static 
-Str
 in
-g
+t
  getLocalVer
@@ -2106,19 +2106,19 @@
 .version
-Nam
+Cod
 e;%0A     
@@ -2215,36 +2215,34 @@
          return 
-null
+-1
 ;%0A        %7D%0A    
@@ -2589,24 +2589,42 @@
  Uri uri) %7B%0A
+        try %7B%0A    
         Inte
@@ -2667,16 +2667,20 @@
 _VIEW);%0A
+    
         
@@ -2746,24 +2746,28 @@
 -archive%22);%0A
+    
         cont
@@ -2789,20 +2789,95 @@
 ty(intent);%0A
+        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%7D%0A

Fix update versionCode gathering