package com.xm.test.myfilemaster.fragment

import android.content.Intent
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 com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.activity.FileManagerActivity
import com.xm.test.myfilemaster.activity.MediaActivity
import com.xm.test.myfilemaster.adapter.MediaAdapter
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.util.MediaUtil
import com.xm.test.myfilemaster.util.PermissionUtil
import com.xm.test.myfilemaster.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)
    }
}
