package jp.yuta.kohashi.fakelineapp.activities

import android.content.Intent
import android.net.Uri
import android.support.v4.app.Fragment
import android.support.v7.app.AlertDialog
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import jp.yuta.kohashi.fakelineapp.R
import jp.yuta.kohashi.fakelineapp.ad.BaseAdBannerActivity
import jp.yuta.kohashi.fakelineapp.managers.FileManager
import jp.yuta.kohashi.fakelineapp.managers.IntentHelper
import jp.yuta.kohashi.fakelineapp.utils.ToastUtil
import jp.yuta.kohashi.fakelineapp.utils.Util
import kotlinx.android.synthetic.main.activity_ad_banner.*


/**
 * Author : yutakohashi
 * Project name : FakeLineApp
 * Date : 07 / 09 / 2017
 */
class ImagePreviewActivity : BaseAdBannerActivity() {

    private var mImageView: ImageView? = null

    // scheme file
    private var mImagePathUri: Uri? = null


    companion object {
        val KEY_IMAGE_PATH: String = "image_path"
    }

    override fun setView(): View? {
        mImageView = ImageView(this)
        mImageView!!.scaleType = ImageView.ScaleType.FIT_CENTER
        mImageView!!.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        return mImageView
    }

    override fun setFragment(): Fragment? = null

    override fun setEvent() {
        toolbarVisibility = true
        toolbarBackable = true
        toolbar.title = ""

        super.setEvent()

        mImagePathUri = Util.strToUri(intent.getStringExtra(KEY_IMAGE_PATH), "file")
        if (mImagePathUri != null) Util.setImageByGlide(mImagePathUri!!, mImageView!!)
        else {
            // TODO toast error when imagepath is empty
            finish()
        }
        fragmentContainer.setBackgroundColor(Util.getColor(R.color.bg_image_preview))
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_image_preview, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.action_share -> actionShare()
            R.id.action_delete -> actionDelete()
        }
        return super.onOptionsItemSelected(item)
    }

    private fun actionShare() {
        mImagePathUri?.let { IntentHelper.actionShare(this@ImagePreviewActivity, it) }
    }

    private fun actionDelete() {
        mImagePathUri?.let {
            AlertDialog.Builder(this)
                    .setTitle(Util.getString(R.string.confirmation))
                    .setMessage(Util.getString(R.string.text_delete_dialog))
                    .setPositiveButton(Util.getString(R.string.yes)) { _, _ ->
                        FileManager(this).deleteFile(it)
                        ToastUtil.doneDelete()
                        finish()
                    }
                    .setNegativeButton(Util.getString(R.string.no), null)
                    .show()
        }
    }

}
