package com.develop.setting.init.ui

import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.UriUtils
import com.blankj.utilcode.util.UtilsFileProvider
import com.develop.food.base.common.LANGUAGE_FROM_SETTING
import com.develop.food.base.common.WIFI_FROM_MAIN_SETTING
import com.develop.food.base.data.DataFactory
import com.develop.food.base.data.SettingType
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.FileKit
import com.develop.food.base.utils.ResDimension
import com.develop.setting.R
import com.develop.setting.databinding.ActivitySettingMainBinding
import com.develop.setting.init.adapter.SettingMainAdapter


@Route(path = Screens.Setting.MAIN_SETTING)
class SettingMainActivity : BaseActivity<ActivitySettingMainBinding>() {
    private var settingMainAdapter = SettingMainAdapter()
    override fun createViewBinding(inflater: LayoutInflater): ActivitySettingMainBinding {
        return ActivitySettingMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.topBar.setTopBarTitle(getString(R.string.settings))
        binding.topBar.listenBackClick {
            finish()
        }

        val manager = GridLayoutManager(this, 2)
        binding.mainSettingRecycler.apply {
            adapter = settingMainAdapter
            layoutManager = manager
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    val srcPosition = parent.getChildAdapterPosition(view)
                    if (srcPosition == 0 || srcPosition == 1) {
                        outRect.top = ResDimension(R.dimen.convert_156px).toInt()
                    }
                    when (srcPosition % 2) {
                        0 -> {
                            outRect.left = ResDimension(R.dimen.convert_40px).toInt()
                        }
                        1 -> {
                            outRect.right = ResDimension(R.dimen.convert_40px).toInt()
                        }
                    }
                    outRect.bottom = ResDimension(R.dimen.convert_120px).toInt()
                }
            })
        }
        val dataList = DataFactory.genSettingModesList(resources)
        settingMainAdapter.setList(dataList)
        settingMainAdapter.setOnItemClickListener { adapter, view, position ->
            when (dataList[position].type) {
                SettingType.BRIGHTNESS -> {
                    navigateTo(Screens.Setting.BRIGHTNESS)
                }
                SettingType.SOUND -> {
                    navigateTo(Screens.Setting.SOUND)
                }
                SettingType.WIFI -> {
                    navigateTo(Screens.Setting.WIFI) {
                        val bundle = Bundle()
                        bundle.putBoolean(WIFI_FROM_MAIN_SETTING, true)
                        with(bundle)
                    }
                }
                SettingType.USER_ACCOUNT -> {
                    navigateTo(Screens.Auth.MEMBER) {
                        withBoolean("enter_from_home", true)
                    }
                }

                SettingType.LANGUAGE -> {
                    navigateTo(Screens.Setting.LANGUAGE) {
                        val bundle = Bundle()
                        bundle.putBoolean(LANGUAGE_FROM_SETTING, true)
                        with(bundle)
                    }
                }
                SettingType.ABOUT -> {
                    navigateTo(Screens.Setting.ABOUT)
                }
                SettingType.RESTORE_FACTORY_SETTINGS -> {
                    navigateTo(Screens.Setting.RESTORE)
                }
                SettingType.STORAGE -> {
                    openFileManager()
                }
            }
        }
    }

    // 打开文件管理器选择文件
    private fun openFileManager() {
        // 打开文件管理器选择文件
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.type = "application/zip"
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        startActivityForResult(intent, 100)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100) {
            data?.data?.apply {
                val path = FileKit.getPath(this@SettingMainActivity,this)
            }

        }
    }


}