package com.test.mydemo3.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.model.FileBean

class ImageShowActivity : AppCompatActivity() {

    private lateinit var mImageShowView: ImageView
    private lateinit var mVideoShowView: VideoView
    private lateinit var mShareBtn: View
    private lateinit var mDeleteBtn: View

    private var mFileBean: FileBean? = null
    private var mFileType: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_show)

        mFileBean = intent.getSerializableExtra("fileBean") as FileBean ?: null
        mFileType = intent.getStringExtra("fileType").toString() ?: ""

        if (!mFileType.isEmpty()){
            when(mFileType){
                "image"->{
                    Glide.with(this).load(mFileBean?.filePath).centerCrop().into(mImageShowView)
                }
                "video"->{
//                    mVideoShowView.
//                    TODO
                }
                else ->{}
            }
        }

        initView()
        setOnClickListener()
    }

    private fun setOnClickListener() {
        mShareBtn.setOnClickListener {
            val uri = Uri.parse(mFileBean?.filePath)
            shareFile(uri)
        }
        mDeleteBtn.setOnClickListener {

        }
    }

    private fun initView() {
        mImageShowView = findViewById(R.id.image_show_view)
        mVideoShowView = findViewById(R.id.video_show_view)
        mShareBtn = findViewById(R.id.image_share_btn)
        mDeleteBtn = findViewById(R.id.image_delete_btn)
    }

    private fun shareFile(uri: Uri) {
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "text/plain"
        intent.putExtra(Intent.EXTRA_STREAM, uri)
        startActivity(Intent.createChooser(intent, "Share File"))
    }
}