package com.key.coolkeyboard.firbase;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.key.coolkeyboard.CoolKeyboardSkin;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class FirebaseEvent {
    private static FirebaseEvent firebaseEventInstance;

    private Context mContext;

    public FirebaseEvent(Context context) {
        this.mContext = context;
    }

    public static FirebaseEvent getInstance(Context context) {
        if (firebaseEventInstance == null) {
            synchronized (FirebaseEvent.class) {
                if (firebaseEventInstance == null) {
                    firebaseEventInstance = new FirebaseEvent(context);
                }
            }
        }
        return firebaseEventInstance;
    }

    public void PostEvent(){
        FirebaseAnalytics instance = FirebaseAnalytics.getInstance(mContext);
        Bundle bundleEvent = new Bundle();
        String initiatingPackageName = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            try {
                initiatingPackageName = mContext.getPackageManager().getInstallSourceInfo(mContext.getApplicationInfo().packageName).getInitiatingPackageName();
            } catch (PackageManager.NameNotFoundException e) {
                Log.d(CoolKeyboardSkin.TAG, "-firstInstall----NameNotFoundException=" + e.getMessage());
            }
        } else {
            initiatingPackageName = mContext.getPackageManager().getInstallerPackageName(mContext.getApplicationInfo().packageName);
        }
        Log.d(CoolKeyboardSkin.TAG, "-firstInstall----initiatingPackageName=" + initiatingPackageName);
        bundleEvent.putString("installer_name", initiatingPackageName);
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo("com.android.vending", 0);
            bundleEvent.putString("ps_version", packageInfo.versionName);
            bundleEvent.putInt("ps_version_code", packageInfo.versionCode);
        } catch (Throwable unused) {
            bundleEvent.putString("ps_version", "-1");
            bundleEvent.putInt("ps_version_code", -1);
        }

        //开机日期的时间戳
        long timeString = System.currentTimeMillis()-SystemClock.elapsedRealtime();
        //开机累计毫秒数
        long millis = System.currentTimeMillis() - timeString;

        String days = convertMillisToDaysHours(System.currentTimeMillis()-timeString);
        bundleEvent.putString("开机",days);


        long lastModified = new File(mContext.getApplicationInfo().sourceDir).lastModified();
        String formatLastModified = getTimeString(lastModified);
        bundleEvent.putString("安装",formatLastModified);

        Log.d(CoolKeyboardSkin.TAG, "-firstInstall----开机=" + days+"----开机毫秒数=--"+millis+"----安装="+formatLastModified);
        instance.logEvent(CoolKeyboardSkin.Firebase_Key, bundleEvent);
    }

    private  String getTimeString(long  tim) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return simpleDateFormat.format(new Date(tim));
    }
    private  String convertMillisToDaysHours(long milliseconds) {
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
        long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);
        long days = TimeUnit.MILLISECONDS.toDays(milliseconds);

        return days + " 天 " + hours % 24 + " 小时 "+minutes % 60+" 分钟";
    }
}
