package com.develop.setting.ui

import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.develop.base.ext.load
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.ext.navigateTo
import com.develop.base.ext.resId2Dimension
import com.develop.base.ext.updateText
import com.develop.base.util.FileKit
import com.develop.common.data_repo.db.DataFactory
import com.develop.common.data_repo.db.SettingModel
import com.develop.common.data_repo.db.SettingType
import com.develop.common.router.Screens
import com.develop.common.tag.LANGUAGE_FROM_SETTING
import com.develop.common.tag.WIFI_FROM_MAIN_SETTING
import com.develop.common.ui.CommonBindingActivity
import com.develop.setting.R
import com.develop.setting.databinding.ActivitySettingMainBinding
import com.drake.brv.utils.grid
import com.drake.brv.utils.models
import com.drake.brv.utils.setup


@Route(path = Screens.Setting.MAIN_SETTING)
class SettingMainActivity : CommonBindingActivity<ActivitySettingMainBinding>() {
    private var dataList = mutableListOf<SettingModel>()

    override fun createViewBinding(inflater: LayoutInflater): ActivitySettingMainBinding {
        return ActivitySettingMainBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataList = DataFactory.genSettingModesList(resources)
        binding.tvTitle.updateText(getString(com.develop.common.R.string.settings))
        binding.ivClose.setOnClickListener {
            finish()
        }
        binding.mainSettingRecycler.apply {
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    outRect.top = dp77
                }
            })
            grid(4)
            setup {
                addType<SettingModel>(R.layout.item_setting_main_view)
                onBind {
                    val model = getModel<SettingModel>()
                    findView<AppCompatTextView>(R.id.tv_name).updateText(model.name)
                    findView<ImageView>(R.id.iv_icon).load(model.resId)
                }
                R.id.iv_icon.onClick {
                    Log.d("dddddd", "---")
                    when (dataList[absoluteAdapterPosition].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()
                        }
                    }
                }

            }
        }.models = dataList
    }

    // 打开文件管理器选择文件
    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)
            }

        }
    }

    companion object {
        var dp156 = (com.develop.base.R.dimen.convert_156px).resId2Dimension().toInt()
        var dp40 = (com.develop.base.R.dimen.convert_40px).resId2Dimension().toInt()
        var dp77 = (com.develop.base.R.dimen.convert_77px).resId2Dimension().toInt()
    }


}