package com.test.mydemo3.antivirus

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.test.mydemo3.MyApplication
import com.test.mydemo3.R
import com.test.mydemo3.activity.ScanResultActivity
import com.trustlook.sdk.cloudscan.CloudScanListener
import com.trustlook.sdk.data.AppInfo

class AntivirusActivity : AppCompatActivity() {

    private lateinit var mPage1: View
    private lateinit var mPage2: View
    private lateinit var mPagerCheck: View
    private lateinit var mProgress: ProgressBar
    private lateinit var mScaningView: View
    private lateinit var mScanSuccessView: View
    private lateinit var mScanAppNameText: TextView
    private var mIntent : Intent? = null
    private var mTotal = "22"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_antivirus)
        mIntent = Intent(this, ScanResultActivity::class.java)
        initView()
        setOnclickListener()
    }

    private fun setOnclickListener() {

        findViewById<View>(R.id.interrupt_cancel_text).setOnClickListener {
            MyApplication.mCloudScan?.cancelScan()
            finish()
        }
        findViewById<View>(R.id.antivirus_quit_btn).setOnClickListener {
            MyApplication.mCloudScan?.cancelScan()
            finish()
        }
        findViewById<View>(R.id.antivirus_quit_btn_2).setOnClickListener {
            MyApplication.mCloudScan?.cancelScan()
            finish()
        }
        findViewById<View>(R.id.Start_scanning).setOnClickListener {
            startAntivirus()
            mPage2.visibility = View.GONE
            mPagerCheck.visibility = View.VISIBLE


        }
        findViewById<View>(R.id.antivirus_stop_text).setOnClickListener {
            MyApplication.mCloudScan?.cancelScan()
            finish()
        }
    }

    private fun initView() {
        mPage1 = findViewById(R.id.antivirus_page_1)
        mPage2 = findViewById(R.id.antivirus_page_2)
        mPagerCheck = findViewById(R.id.check_the_source_layout)
        mProgress = findViewById(R.id.antivirus_progress)
        mScaningView = findViewById(R.id.scan_ing_layout)
        mScanSuccessView = findViewById(R.id.antivirus_success_layout)
        mScanAppNameText = findViewById(R.id.scan_name_text)
    }

    private fun startAntivirus(){
        MyApplication.mCloudScan?.startQuickScan(object : CloudScanListener(){
            override fun onScanStarted() {
                Log.e("yanglin", "onScanStarted: start", )
            }

            override fun onScanProgress(curr: Int, total: Int, result: AppInfo?) {
                    Log.e("yanglin", "onScanProgress, " + "curr=" + curr + " total=" + total + " app name=" +
                            result?.appName, )
                mScanAppNameText.text = result?.appName
                mProgress.setProgress(curr)
                mTotal = total.toString()

            }

            override fun onScanError(errCode: Int, p1: String?) {
                Log.e("yanglin", "onScanError=" + errCode, )
            }

            override fun onScanCanceled() {
                Log.e("yanglin", "onScanStarted: cancel", )
            }

            override fun onScanInterrupt() {
                Log.e("yanglin", "onScanStarted: onScanInterrupt", )
            }

            override fun onScanFinished(result: MutableList<AppInfo>?) {
                Log.e("yanglin", "onScanFinished: start" + result!![0].toString(), )
                mProgress.setProgress(100)
                mScaningView.visibility = View.GONE
                mScanSuccessView.visibility = View.VISIBLE
                val appInfos = ArrayList<AppInfo>()
                val bundle = Bundle()
                bundle.putSerializable("result", appInfos)
                mIntent?.putExtra("total", mTotal)
                for (appInfo in result){
                    appInfos.add(appInfo)
                }
                Thread(Runnable {
                    Handler(Looper.getMainLooper()).postDelayed(Runnable {
                        mIntent?.putExtras(bundle)
                        startActivity(mIntent)
                        finish()
                    }, 2000)
                }).start()
            }

        })
    }
}