package cn.muchinfo.rma.view.autoWidget.photopicter;

import android.content.Intent
import android.os.Bundle

import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import cn.muchinfo.rma.R
import cn.muchinfo.rma.view.autoWidget.commonLeftButton
import cn.muchinfo.rma.view.autoWidget.commonMenuButton
import cn.muchinfo.rma.view.autoWidget.commonTitle
import cn.muchinfo.rma.view.autoWidget.photopicter.PhotoPreview.*
import cn.muchinfo.rma.view.autoWidget.photopicter.adapter.PhotoPagerAdapter
import cn.muchinfo.rma.view.autoWidget.topBar
import com.bumptech.glide.Glide
import com.google.android.material.snackbar.Snackbar


import org.jetbrains.anko.matchParent
import org.jetbrains.anko.support.v4.viewPager
import org.jetbrains.anko.verticalLayout


/**
 * 预览图片用的界面
 * Created by TsubasaP91 on 15/6/24.
 */
class PhotoPagerActivity : AppCompatActivity() {

    private val mCurrentItem by lazy { intent.getIntExtra(EXTRA_CURRENT_ITEM, 0) }
    private val paths by lazy { intent.getStringArrayListExtra(EXTRA_PHOTOS) }
    private val showDelete: Boolean by lazy { intent.getBooleanExtra(EXTRA_SHOW_DELETE, true) }
    private lateinit var tvTitle: TextView
    private lateinit var mViewPager: ViewPager
    private val mPagerAdapter: PhotoPagerAdapter by lazy { PhotoPagerAdapter(Glide.with(this), paths) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        verticalLayout {
            background = resources.getDrawable(R.color.segtabment_bg_color)
            topBar {
                commonLeftButton()
                commonTitle {
                    tvTitle = this
                }
                if (showDelete) commonMenuButton(R.drawable.__picker_delete) { deleteImage() }
            }
            viewPager {
                mViewPager = this
                adapter = mPagerAdapter
                currentItem = mCurrentItem
                addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
                    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                        if (paths?.size == 1){
                            tvTitle.text = "附件预览"
                        }else{
                            tvTitle.text = getString(R.string.__picker_image_index, currentItem + 1, paths?.size)
                        }
                    }
                })
            }.lparams(matchParent, 0, 1f)

        }
    }

    override fun onBackPressed() {
        val intent = Intent()
        intent.putExtra(PhotoPicker.KEY_SELECTED_PHOTOS, paths)
        setResult(RESULT_OK, intent)
        finish()
        super.onBackPressed()
    }

    private fun deleteImage() {
        val index = mViewPager.currentItem
        val deletedPath = paths?.get(index)
        val snackbar = Snackbar.make(mViewPager, R.string.__picker_deleted_a_photo,
                Snackbar.LENGTH_LONG)
        if (paths?.size ?: 0 <= 1) {
            // autoDismissAfterShow confirm dialog
            AlertDialog.Builder(this)
                    .setTitle(R.string.__picker_confirm_to_delete)
                    .setPositiveButton(R.string.__picker_yes) { dialogInterface, i ->
                        dialogInterface.dismiss()
                        paths?.removeAt(index)
                        mPagerAdapter.notifyDataSetChanged()
                        onBackPressed()
                    }
                    .setNegativeButton(R.string.__picker_cancel) { dialogInterface, i -> dialogInterface.dismiss() }
                    .show()
        } else {
            snackbar.show()
            paths?.removeAt(index)
            mPagerAdapter.notifyDataSetChanged()
        }
        snackbar.setAction(R.string.__picker_undo) {
            if (paths?.size ?: 0 > 0) {
                paths?.add(index, deletedPath)
            } else {
                paths?.add(deletedPath)
            }
            mPagerAdapter.notifyDataSetChanged()
            mViewPager.setCurrentItem(index, true)
        }
    }

}
