package com.duolebo.blyrobot.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.*

import com.duolebo.blyrobot.R
import com.minhui.vpn.utils.ThreadProxy

import com.duolebo.blyrobot.data.ApkPackageInfo


class PackageListActivity : AppCompatActivity() {
    private var packageListView: GridView? = null
    private var apkPackageInfo: List<ApkPackageInfo>? = null
    internal lateinit var pm: PackageManager
    private var pg: ProgressBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_package_list)
        supportActionBar!!.title = "选择需要抓包的程序"
        pg = findViewById(R.id.pg)

        pm = packageManager
        packageListView = findViewById(R.id.package_list)
        ThreadProxy.getInstance().execute(object : Runnable {

            private var showPackageAdapter: ShowPackageAdapter? = null

            override fun run() {
                apkPackageInfo = ApkPackageInfo.getAppInfos(applicationContext)
                showPackageAdapter = ShowPackageAdapter()
                runOnUiThread {
                    packageListView!!.adapter = showPackageAdapter
                    pg!!.visibility = View.GONE
                }
            }
        })

        packageListView!!.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            val intent = Intent()
            if (position != 0) {
                intent.putExtra(SELECT_PACKAGE, apkPackageInfo!![position])
            }
            setResult(AppCompatActivity.RESULT_OK, intent)
            finish()
        }
    }

    internal inner class ShowPackageAdapter : BaseAdapter() {
        private var defaultDrawable: Drawable = resources.getDrawable(R.drawable.sym_def_app_icon)

        override fun getCount(): Int {
            return apkPackageInfo!!.size
        }

        override fun getItem(position: Int): Any? {
            return null
        }

        override fun getItemId(position: Int): Long {

            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            var convertView = convertView
            val holder: Holder
            if (convertView == null) {
                convertView = View.inflate(this@PackageListActivity, R.layout.item_select_package, null)
                holder = Holder(convertView!!, position)
                convertView.tag = holder
            } else {
                holder = convertView.tag as Holder
                holder.holderPosition = position
            }

            val packageInfo = this@PackageListActivity.apkPackageInfo!![position]
            if (packageInfo.appName == null) {
                holder.appName.text = packageInfo.packageName
            } else {
                holder.appName.text = packageInfo.appName
            }
            holder.icon.setImageDrawable(defaultDrawable)
            val alertIconView = convertView
            ThreadProxy.getInstance().execute(Runnable {
                val iconHolder = alertIconView.tag as Holder
                if (iconHolder.holderPosition != position) {
                    return@Runnable
                }
                val drawable = packageInfo.applicationInfo.loadIcon(pm)
                runOnUiThread(Runnable {
                    val iconHolder = alertIconView.tag as Holder
                    if (iconHolder.holderPosition != position) {
                        return@Runnable
                    }
                    holder.icon.setImageDrawable(drawable)
                })
            })
            return convertView
        }

        internal inner class Holder(baseView: View, var holderPosition: Int) {
            var appName: TextView = baseView.findViewById(R.id.app_name)
            var icon: ImageView = baseView.findViewById(R.id.select_icon)

        }
    }

    companion object {
        const val SELECT_PACKAGE = "package_select"
    }

}
