package com.tcl.customerapi;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import com.tcl.appassistsdk.appupdate.AppUpdateManager;
import com.tcl.appassistsdk.appupdate.UpdateConstant;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.snack.net.Nets;

import org.jetbrains.annotations.NotNull;

public class AppUpdateService extends Service {
    private String TAG = AppUpdateService.class.getName();
    private Context ctx;
    private boolean currentNetworkStatus = false;

    public AppUpdateService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "service  onCreate  !");
        //add Application Self-upgrade function
        ctx = this;
        startCheckAppUpdate();
    }

    private void startCheckAppUpdate(){
        int count = 0;
        while ((!currentNetworkStatus) && count < 3) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
                    if (connectivityManager != null) {
                        connectivityManager.requestNetwork(new NetworkRequest.Builder().build(),
                                new ConnectivityManager.NetworkCallback() {
                                    @Override
                                    public void onAvailable(@NotNull Network network) {
                                        super.onAvailable(network);
                                        Log.i(TAG, "network state changed");
                                        currentNetworkStatus = true;
                                    }
                                });
                    }
                }
                Thread.sleep(10 * 1000);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            count++;
            Log.i(TAG, "currentNetworkStatus " + currentNetworkStatus + " count " + count);
        }
        try {
            AppUpdateManager manager = AppUpdateManager.getInstance(ctx,
                    UpdateConstant.UpdateType.SILENT);
            PackageInfo packageInfo = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_CONFIGURATIONS);
            String projectName = ThirdParty.getModelName();
            String softVersion = ThirdParty.getSoftwareVersion();
            String corePlatform = "";
            if (softVersion != null && softVersion.startsWith("V8-") && softVersion.length() > 7) {
                corePlatform = softVersion.substring("v8-".length(), 7);
            }
            manager.checkUpdate(getPackageName(), String.valueOf(packageInfo.versionCode), packageInfo.versionName, Nets.getEthMacAddress(false), projectName, corePlatform, softVersion);
            Log.i(TAG, "add Application Self-upgrade function");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG, "service  onDestroy  !");
    }
}
