package com.zswc.lookimg

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.qmuiteam.qmui.kotlin.onClick
import com.quansu.lookimg.R
import com.quansu.lookimg.databinding.ActivityImagepagerBinding
import com.zswc.lookimg.adapter.ImagePagerAdapter
import com.zswc.lookimg.vmodel.ImagePagerVModel
import com.ysnows.base.base.BActivity
import java.util.*

/**
 *Created by shihuiyun
 *on 2020/9/18
 */
class ImagePagerActivity : BActivity<ImagePagerVModel, ActivityImagepagerBinding>() {


    var adapter: ImagePagerAdapter? = null
    var urls: ArrayList<String>? = null
    private var position = 0


    override fun __before() {
        super.__before()

        urls = intent.extras?.getStringArrayList("urls")
        position = intent.extras?.getInt("position")!!


    }


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        initViewPager()

        binding.ivBack.onClick {

            finish()
        }
    }


    private fun initViewPager() {
        adapter = ImagePagerAdapter(supportFragmentManager, urls!!)
        binding.pager.adapter = adapter

        val text: CharSequence = getString(R.string.viewpager_indicator, 1, binding.pager.adapter!!.count)
        if (urls!!.size == 1) binding.indicator.visibility = View.GONE else binding.indicator.visibility = View.VISIBLE


        vm.text.value = text.toString()


        binding.pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

                val text: CharSequence = getString(R.string.viewpager_indicator, position + 1, binding.pager.adapter!!.count)
                vm.text.value = text.toString()


            }

        })

        binding.pager.currentItem = position


    }

    override fun vmClass(): Class<ImagePagerVModel> {
        return ImagePagerVModel::class.java
    }


    override fun binding(): ActivityImagepagerBinding {
        return ActivityImagepagerBinding.inflate(layoutInflater)
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event!!.action == KeyEvent.ACTION_UP) {
            finish()
            //不执行父类点击事件
            return true
        }


        return super.onKeyDown(keyCode, event)
    }


    override fun title(): String? {
        return null
    }

}
