package com.xm.test.myfilemaster.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.trustlook.sdk.data.AppInfo
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.adapter.ScanResultAdapter
import com.xm.test.myfilemaster.view.CustomDialog

class ScanResultActivity : AppCompatActivity() {

    private var mResult: ArrayList<AppInfo>? = null
    private lateinit var mListView: ListView
    private lateinit var mAppinfoPage: View
    private lateinit var mDeleteGroupView: View
    private lateinit var mGoItBtn: TextView
    private lateinit var mScanResultListLyout: View
    private lateinit var mAntivirusSuccessView: View
    private var mScanResultAdapter: ScanResultAdapter? = null
    private lateinit var mIdentifyText: TextView
    private lateinit var mSettleText: TextView

    private var mCurrentAppInfo: AppInfo? = null

    private var mCurr = 0
    private var mIgnoreCount = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan_result)

        mResult = intent.getSerializableExtra("result") as ArrayList<AppInfo>
        mCurr = intent.getIntExtra("curr", 0)
        findViewById<TextView>(R.id.scan_result_secure_num_text).text = intent.getStringExtra("total")
        initView()
        setViewClickListener()
    }

    private fun setViewClickListener() {
        mListView.setOnItemClickListener { parent, view, position, id ->
//            Intent(this, )
            val appIcon = findViewById<ImageView>(R.id.scan_app_info_icon)
            val appName = findViewById<TextView>(R.id.scan_app_info_name)
            val appDate = findViewById<TextView>(R.id.scan_app_info_date)
            val packageInfo = packageManager.getPackageInfo(
                mResult!![position].packageName,
                PackageManager.GET_META_DATA
            )
            appIcon.setImageDrawable(packageInfo.applicationInfo.loadIcon(packageManager))
            appName.text = mResult!![position].appName
            mCurrentAppInfo = mResult!![position]
//            mResult!![position]
            mAppinfoPage.visibility = View.VISIBLE
            mDeleteGroupView.visibility = View.VISIBLE
            mScanResultListLyout.visibility = View.GONE
            mGoItBtn.visibility = View.GONE
        }

        findViewById<View>(R.id.scan_delete_yes).setOnClickListener {
            val customDialog = CustomDialog(this)
            customDialog.setsCancel(View.OnClickListener {
                customDialog.dismiss()
            }).setsConfirm(View.OnClickListener {
                Intent(Intent.ACTION_DELETE, Uri.parse("package:${mCurrentAppInfo!!.packageName}"))
                startActivity(intent)
                mResult?.remove(mCurrentAppInfo!!)
            }).show()
            customDialog.isShowEditText(false)
            customDialog.setsTitle("Delete")
            customDialog.setsHint("Do you want to uninstall this app?")
            customDialog.setsRightDelete("Delete")

        }
        findViewById<View>(R.id.scan_delete_ignore).setOnClickListener {

            val customDialog = CustomDialog(this)
            customDialog.setsCancel(View.OnClickListener {
                customDialog.dismiss()
            }).setsConfirm(View.OnClickListener {
                mIgnoreCount++
                mResult?.remove(mCurrentAppInfo!!)
                mAppinfoPage.visibility = View.GONE
                mDeleteGroupView.visibility = View.GONE
                mGoItBtn.visibility = View.VISIBLE
                mScanResultListLyout.visibility = View.VISIBLE
                mScanResultAdapter?.notifyDataSetChanged()
                customDialog.dismiss()
            }).show()
            customDialog.isShowEditText(false)
//            customDialog.setsTitle("Delete")
            customDialog.setShowTitle(false)
            customDialog.setsHint("Are you sure you want to ignore the app?")
            customDialog.setRightBtnText("Ignore")

        }
        findViewById<View>(R.id.antivirus_success_quit_btn).setOnClickListener {
            finish()
        }
        findViewById<View>(R.id.scan_result_go_it_text).setOnClickListener {
            mSettleText.text = mIgnoreCount.toString()
            mAntivirusSuccessView.visibility = View.VISIBLE
        }
        findViewById<View>(R.id.scan_result_quit_btn).setOnClickListener {
            finish()
        }

        findViewById<View>(R.id.scan_result_finish_text).setOnClickListener {
            val intent = Intent(this, ResultPageActivity::class.java)
            intent.putExtra("pageTitle", "Antivirus")
            intent.putExtra("pageName", "antivirus")
            intent.putExtra("pageSuccessText", "No risks were identified")
            startActivity(intent)
            finish()
        }
    }

    private fun initView() {
        mListView = findViewById(R.id.scan_result_list)
        mAppinfoPage = findViewById(R.id.app_info_page_layout)
        mDeleteGroupView = findViewById(R.id.scan_result_delete_group_layout)
        mGoItBtn = findViewById(R.id.scan_result_go_it_text)
        mScanResultListLyout = findViewById(R.id.scan_result_list_layout)
        mAntivirusSuccessView = findViewById(R.id.antivirus_success_view)
        mIdentifyText = findViewById(R.id.identify_num_text)
        mSettleText = findViewById(R.id.settle_num_text)

        mIdentifyText.text = mCurr.toString()

        mScanResultAdapter = ScanResultAdapter(mResult!!, this)

        mListView.adapter = mScanResultAdapter
    }

    override fun onBackPressed() {
        if (mAppinfoPage.isVisible){
            mAppinfoPage.visibility = View.GONE
            mDeleteGroupView.visibility = View.GONE
            mGoItBtn.visibility = View.VISIBLE
            mScanResultListLyout.visibility = View.VISIBLE
        }else{
            super.onBackPressed()
        }
    }
}