package com.xm.test.myfilemaster.activity

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 var mScanResultAdapter: ScanResultAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan_result)

        mResult = intent.getSerializableExtra("result") as ArrayList<AppInfo>
        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)
            appName.text = mResult!![position].appName
//            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 {

            }).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 {

        }
    }

    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)

        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()
        }
    }
}