package com.lukouguoji.module_base.ui.page.preview

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import com.lukouguoji.module_base.R
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.FileBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.databinding.ActivityPreviewBinding
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.EmptyViewModel
import com.lukouguoji.module_base.ktx.toJson
import dev.utils.app.BarUtils

/**
 * 预览界面
 */
class PreviewActivity : BaseBindingActivity<ActivityPreviewBinding, EmptyViewModel>() {

    // 适配器
    private val adapter by lazy {
        CommonAdapter(
            requireContext(), R.layout.item_preview_image, PreviewImageViewHolder::class.java
        )
    }

    override fun layoutId() = R.layout.activity_preview
    override fun viewModelClass() = EmptyViewModel::class.java
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        resources?.let {
            if(it.displayMetrics.widthPixels < it.displayMetrics.heightPixels){
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }else{
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            }
        }
    }

    @SuppressLint("SourceLockedOrientationActivity")
    override fun initOnCreate(savedInstanceState: Bundle?) {
        BarUtils.transparentStatusBar(this)
        // 获取传递过来的数据
        intent.getStringExtra(Constant.Key.DATA)?.let {
            val list = NetApply.gson.fromJson(it, Array<FileBean>::class.java).toList()
            adapter.refresh(list)
        }
        // 设置适配器
        binding.vp.adapter = adapter

        // 设置当前显示的位置
        binding.vp.currentItem = intent.getIntExtra(Constant.Key.POSITION, 0)

        // 关闭
        binding.ivClose.setOnClickListener {
            finish()
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context, list: List<FileBean>, position: Int = 0) {
            val starter = Intent(context, PreviewActivity::class.java)
                .putExtra(Constant.Key.DATA, list.toJson(false))
                .putExtra(Constant.Key.POSITION, position)
            context.startActivity(starter)
        }
    }
}