package com.duolebo.blyrobot.data

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Parcel
import android.os.Parcelable

import java.util.ArrayList
import java.util.Collections
import java.util.Comparator

class ApkPackageInfo : Parcelable {
    var appName: String? = null
    lateinit var packageName: String
    lateinit var applicationInfo: ApplicationInfo

    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeString(this.appName)
        dest.writeString(this.packageName)
        dest.writeParcelable(this.applicationInfo, flags)
    }

    constructor() {}

    protected constructor(ins: Parcel) {
        this.appName = ins.readString()
        this.packageName = ins.readString().toString()
        this.applicationInfo = ins.readParcelable(ApplicationInfo::class.java.classLoader)!!
    }

    companion object {
        private val NO_APP_NAME = "COM."

        fun getAppInfos(context: Context): List<ApkPackageInfo> {
            val appInfos = ArrayList<ApkPackageInfo>()
            val packageManager = context.packageManager
            val installedPackages = packageManager.getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES)

            for (info in installedPackages) {
//                if (info.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM <= 0) {
//
//                }
                val packageShowInfo = ApkPackageInfo()
                packageShowInfo.packageName = info.packageName

                packageShowInfo.appName = info.applicationInfo.loadLabel(packageManager) as String

                packageShowInfo.applicationInfo = info.applicationInfo
                appInfos.add(packageShowInfo)
            }

            Collections.sort(appInfos, Comparator { o1, o2 ->
                if (o1 === o2) {
                    return@Comparator 0
                }
                if (o1.appName == null && o2.appName == null) {
                    return@Comparator o1.packageName.toUpperCase().compareTo(o2.packageName.toUpperCase())
                }
                if (o1.appName == null) {
                    return@Comparator -1
                }
                if (o2.appName == null) {
                    return@Comparator 1
                }
                if (o1.appName!!.toUpperCase().startsWith(NO_APP_NAME) && !o2.appName!!.toUpperCase().startsWith(NO_APP_NAME)) {
                    return@Comparator 1
                }
                if (!o1.appName!!.toUpperCase().startsWith(NO_APP_NAME) && o2.appName!!.toUpperCase().startsWith(NO_APP_NAME)) {
                    -1
                } else o1.appName!!.toUpperCase().compareTo(o2.appName!!.toUpperCase())
            })

            return appInfos
        }

        @JvmField
        val CREATOR: Parcelable.Creator<ApkPackageInfo> = object : Parcelable.Creator<ApkPackageInfo> {
            override fun createFromParcel(source: Parcel): ApkPackageInfo {
                return ApkPackageInfo(source)
            }

            override fun newArray(size: Int): Array<ApkPackageInfo?> {
                return arrayOfNulls(size)
            }
        }
    }
}
