package com.xm.test.myfilemaster.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.ListView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.cloud.cleanjunksdk.similar.PicSimilarInfo
import com.cloud.cleanjunksdk.task.SimilarScanCallBack
import com.xm.test.myfilemaster.MyApplication
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.adapter.ImageItemAdapter
import com.xm.test.myfilemaster.adapter.SimilarBeanAdapter
import com.xm.test.myfilemaster.base.BaseActivity
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.view.CustomDialog
import java.io.File

class SimilarImagesActivity : AppCompatActivity(), OnClickListener{

    private lateinit var mTitleText: TextView
    private lateinit var mShareBtn: ImageView
    private lateinit var mDeleteBtn: ImageView
    private lateinit var mNewBtn: ImageView
    private lateinit var mCancelBtn: ImageView
    private lateinit var mQuitBtn: ImageView
    private lateinit var mEditBtn: ImageView
    private lateinit var mBottomBar: View
    private lateinit var mSelectNumText: TextView
    private lateinit var mCheckItemSelectAllBtn: CheckBox
    private lateinit var mSwitchSelectView: RelativeLayout
    private lateinit var mStartAnimation: View

    private lateinit var mListView: ListView
    private lateinit var mRecycleView: RecyclerView

    private lateinit var mFileCheckedItemList: ArrayList<String>

    private var mImageItemAdapter: ImageItemAdapter? = null

    private var mSimilarBeanAdapter: SimilarBeanAdapter? = null


    private var mDataBeans = ArrayList<ArrayList<FileBean>>()
    private var mSimilarDatas = ArrayList<PicSimilarInfo>()


    companion object{
        var isClickEditBtn: Boolean = false
        var switchNumber: Int = 0
        var isClickCheckBoxAllBtn: Boolean = false
        lateinit var mBaseViewModel : BaseActivityViewModel
    }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_duplicate_images)
        initView()
        findViewById<View>(R.id.current_file_path).visibility = View.GONE

    }

    private fun initView() {
        mTitleText = findViewById(R.id.title_bar_text)
        mShareBtn = findViewById(R.id.btn_share)
        mDeleteBtn = findViewById(R.id.btn_delete)
        mNewBtn = findViewById(R.id.btn_new)
        mCancelBtn = findViewById(R.id.btn_cancel)
        mQuitBtn = findViewById(R.id.btn_quit)
        mEditBtn = findViewById(R.id.btn_edit)
        mBottomBar = findViewById(R.id.bottom_btn_group)
        mSwitchSelectView = findViewById(R.id.switch_and_select_layout)
        mSelectNumText = findViewById(R.id.select_num)
        mCheckItemSelectAllBtn = findViewById(R.id.checkbox_item_all)
        mStartAnimation = findViewById(R.id.similar_start_animation)

        mCancelBtn.setOnClickListener(this)
        mQuitBtn.setOnClickListener(this)
        mEditBtn.setOnClickListener(this)
        mCheckItemSelectAllBtn.setOnClickListener(this)

        mListView = findViewById(R.id.files_item_listview)
        mRecycleView = findViewById(R.id.recy_cler_v)

        mSimilarBeanAdapter = SimilarBeanAdapter(this)
        mRecycleView.adapter = mSimilarBeanAdapter


//        mImageItemAdapter = ImageItemAdapter(this, mDataBeans )
//        mListView.adapter = mImageItemAdapter
        startDuplicateImages()

        mBaseViewModel = ViewModelProvider(this)[BaseActivityViewModel::class.java]
        mBaseViewModel.setNumber(0)
        mBaseViewModel.myInt.observe(this, Observer {
            mSelectNumText.text = String.format(getString(R.string.select_0, BaseActivity.switchNumber.toString()))
        })

    }

    private fun startDuplicateImages() {

        MyApplication.mCleanSdk?.startSimilarScan(object : SimilarScanCallBack {
            override fun onSmilarleError(p0: Int) {
                println("error : $p0")
                runOnUiThread {
                    mStartAnimation.visibility = View.GONE
                }
            }

            override fun onSmilarEmitOne(p0: PicSimilarInfo?) {
                var listPath = ArrayList<FileBean>()
//                    for (i in p0?.getmList()!!) {
//                        val fileBean = FileBean()
//                        fileBean.filePath = i.path
//                        listPath.add(fileBean)
//                    }
//                    mDataBeans.add(listPath)
                mSimilarDatas.add(p0!!)

            }

            override fun onSmilarScanFinish() {
                runOnUiThread {
                    mStartAnimation.visibility = View.GONE
                    mEditBtn.visibility = View.VISIBLE
//                        if (mDataBeans.size == 0) {
                    if (mSimilarDatas.size == 0) {
                        findViewById<View>(R.id.not_file_found_layout).visibility = View.VISIBLE
                    }else{
//                            mImageItemAdapter?.setData(mDataBeans)
                        Log.e("yanglin", "onSmilarScanFinish: mSimilarDatas.size = ${mSimilarDatas.size}", )
                        mSimilarBeanAdapter?.setData(mSimilarDatas)
                    }
                }

            }
        }, 15)

    }

    override fun onClick(v: View?) {

        when(v?.id){
            R.id.btn_cancel ->{
                cancel()
            }
            R.id.btn_quit ->{
                finish()
            }
            R.id.btn_new ->{
//                createNewDir()
            }
            R.id.btn_edit ->{
                mNewBtn.visibility = View.GONE
                mQuitBtn.visibility = View.GONE
                mEditBtn.visibility =View.GONE

                mDeleteBtn.visibility = View.VISIBLE
                mCancelBtn.visibility = View.VISIBLE
//                mShareBtn.visibility = View.VISIBLE
//                mBottomBar.visibility = View.VISIBLE
                mSwitchSelectView.visibility = View.VISIBLE

                isClickEditBtn = true
            }
            R.id.btn_delete ->{
                val customDialog = CustomDialog(this)
                customDialog.setsCancel(View.OnClickListener {
                    customDialog.dismiss()
                }).setsConfirm(View.OnClickListener {
                    for (fileBean in mImageItemAdapter?.mCheckedLists!!){
                        MyApplication.mCleanSdk?.delete(File(fileBean.filePath))
                    }
                    startDuplicateImages()
                }).show()
                customDialog.isShowEditText(false)
                customDialog.setsTitle("Delete")
                customDialog.setsHint("Are you sure you want to delete ${mImageItemAdapter?.mCheckedLists!!.size} item?")

            }
            R.id.checkbox_item_all ->{
                isClickCheckBoxAllBtn = !isClickCheckBoxAllBtn
                mCheckItemSelectAllBtn.isChecked = isClickCheckBoxAllBtn
//                mImageItemAdapter?.selectAll(isClickCheckBoxAllBtn)
                mSimilarBeanAdapter?.selectAll(isClickCheckBoxAllBtn)

            }
        }
    }

    fun cancel(){
//        setStateCheckedMap(false)
        mNewBtn.visibility = View.VISIBLE
        mQuitBtn.visibility = View.VISIBLE
        mEditBtn.visibility =View.VISIBLE
        mCancelBtn.visibility = View.GONE
        mDeleteBtn.visibility = View.GONE
        mShareBtn.visibility = View.GONE
        mBottomBar.visibility = View.GONE
        mNewBtn.visibility = View.GONE
        mSwitchSelectView.visibility = View.GONE

        isClickEditBtn = false
        switchNumber = 0
//        if (mFileAdapter != null){
//            mFileAdapter.notifyDataSetChanged()
//        }
    }







}