package com.test.mydemo3.fragment

import android.content.Intent
import androidx.fragment.app.viewModels
import android.os.Bundle
import android.util.SparseBooleanArray
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.test.mydemo3.R
import com.test.mydemo3.activity.FileManagerActivity
import com.test.mydemo3.activity.MediaActivity
import com.test.mydemo3.adapter.MediaAdapter
import com.test.mydemo3.model.FileBean
import com.test.mydemo3.util.FileUtil
import com.test.mydemo3.util.MediaUtil
import com.test.mydemo3.util.PermissionUtil
import com.test.mydemo3.view.CustomDialog

class FileManagerFragment : Fragment(), View.OnClickListener {
    private lateinit var mView: View

    private lateinit var mImageBtn: View
    private lateinit var mVideoBtn: View
    private lateinit var mAudioBtn: View
    private lateinit var mZipBtn: View
    private lateinit var mApkBtn: View
    private lateinit var mLogBtn: View
    private lateinit var mAllBtn: View
    private lateinit var mWordBtn: View
    private lateinit var mExcelBtn: View
    private lateinit var mPptBtn: View
    private lateinit var mPdfBtn: View
    private lateinit var mTxtBtn: View
    private lateinit var mRecentGrid: GridView
    private lateinit var mRecentView: View

    private var mFileBeanList: ArrayList<FileBean>? = null

    private var mIntent: Intent? = null

    private var mRecentAdapter: MediaAdapter? = null

    private var mRecentImages = ArrayList<FileBean>()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mView = inflater.inflate(R.layout.fragment_file_manager, container, false)

        initView()
        return mView
    }

    private fun initView() {
        mImageBtn = mView.findViewById(R.id.btn_image)
        mVideoBtn = mView.findViewById(R.id.btn_video)
        mAudioBtn = mView.findViewById(R.id.btn_audio)
        mZipBtn = mView.findViewById(R.id.btn_zip)
        mApkBtn = mView.findViewById(R.id.btn_apk)
        mLogBtn = mView.findViewById(R.id.btn_log_file)
        mAllBtn = mView.findViewById(R.id.btn_all)
        mWordBtn = mView.findViewById(R.id.btn_word)
        mExcelBtn = mView.findViewById(R.id.btn_excel)
        mPptBtn = mView.findViewById(R.id.btn_ppt)
        mPdfBtn = mView.findViewById(R.id.btn_pdf)
        mTxtBtn = mView.findViewById(R.id.btn_txt)
        mRecentGrid = mView.findViewById(R.id.recent_file_grid)
        mRecentView = mView.findViewById(R.id.recent_file_view)

        mImageBtn.setOnClickListener(this)
        mVideoBtn.setOnClickListener(this)
        mAudioBtn.setOnClickListener(this)
        mZipBtn.setOnClickListener(this)
        mApkBtn.setOnClickListener(this)
        mLogBtn.setOnClickListener(this)
        mAllBtn.setOnClickListener(this)
        mWordBtn.setOnClickListener(this)
        mExcelBtn.setOnClickListener(this)
        mPptBtn.setOnClickListener(this)
        mPdfBtn.setOnClickListener(this)
        mTxtBtn.setOnClickListener(this)
        mRecentView.setOnClickListener(this)

        mIntent = Intent(context, FileManagerActivity::class.java)
        mImageBtn.setOnClickListener{
            val intent = Intent(context, MediaActivity::class.java)
            intent.putExtra("fileType", "image")
            context?.startActivity(intent)

        }
        mVideoBtn.setOnClickListener{
            val intent = Intent(context, MediaActivity::class.java)
            intent.putExtra("fileType", "video")
            context?.startActivity(intent)
        }
        mRecentView.setOnClickListener {
            val intent = Intent(context, MediaActivity::class.java)
            intent.putExtra("fileType", "image")
            context?.startActivity(intent)
        }
        val sparseBooleanArray = SparseBooleanArray()
        mRecentAdapter = MediaAdapter(mRecentImages, requireContext(), sparseBooleanArray, "image", 80)
        mRecentAdapter?.setItemHeight(80)
        mRecentGrid.adapter = mRecentAdapter


    }

    override fun onClick(v: View?) {
        if (checkPermission()){
            when(v?.id){
                R.id.btn_audio->{mIntent?.putExtra("fileType", "audio")}
                R.id.btn_zip->{mIntent?.putExtra("fileType", "zip")}
                R.id.btn_apk->{mIntent?.putExtra("fileType", "apk")}
                R.id.btn_log_file->{mIntent?.putExtra("fileType", "log")}
                R.id.btn_all->{mIntent?.putExtra("fileType", "all")}
                R.id.btn_word->{mIntent?.putExtra("fileType", "word")}
                R.id.btn_excel->{mIntent?.putExtra("fileType", "excel")}
                R.id.btn_ppt->{mIntent?.putExtra("fileType", "ppt")}
                R.id.btn_pdf->{mIntent?.putExtra("fileType", "pdf")}
                R.id.btn_txt->{mIntent?.putExtra("fileType", "txt")}
            }
            context?.startActivity(mIntent)
        }
    }

    private fun checkPermission(): Boolean {
        if (!PermissionUtil.checkGrantedPermission(activity)) {
            val customDialog = CustomDialog(context)
            customDialog.setsConfirm(View.OnClickListener {
                PermissionUtil.requestPermission(context)
                customDialog.dismiss()
            }).setsCancel(View.OnClickListener {
                customDialog.dismiss()
            }).show()
            customDialog.isShowEditText(false)
            customDialog.setsTitle("not permission")
            customDialog.setsHint("Please grant file management permissions")
            return false
        }
        return true
    }

    override fun onResume() {
        super.onResume()
        mRecentImages = MediaUtil.getRecentImages(requireContext(), 4)
        mRecentAdapter?.setData(mRecentImages)
    }
}
