package xyz.myachin.saveto.ui.sharing.select

import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import xyz.myachin.saveto.logic.intent.ACTION_DOWNLOADED
import xyz.myachin.saveto.logic.intent.ACTION_DOWNLOADED_MANUAL_CHOOSER

class ChooserShareActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if ((intent?.action == ACTION_DOWNLOADED_MANUAL_CHOOSER ||
                    intent?.action == ACTION_DOWNLOADED ||
                    intent?.action == Intent.ACTION_SEND)
            && !intent?.type.isNullOrEmpty()
        ) {
            handleIntent(intent)
            return
        }
        finish()
    }

    private fun handleIntent(intent: Intent) {
        when (intent.type) {
            "application/x-bzip",
            "application/x-bzip2",
            "application/gzip",
            "application/vnd.rar",
            "application/x-tar",
            "application/zip",
            "application/x-7z-compressed" -> {
                intent.component = ComponentName.createRelative(
                    applicationContext,
                    ChooserArchiveActivity::class.java.name
                )
            }
            "text/plain",
            "application/x-abiword",
            "application/msword",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            "application/vnd.oasis.opendocument.presentation",
            "application/vnd.oasis.opendocument.spreadsheet",
            "application/vnd.oasis.opendocument.text",
            "application/pdf",
            "application/rtf",
            "application/vnd.ms-powerpoint",
            "application/vnd.openxmlformats-officedocument.presentationml.presentation",
            "application/vnd.visio",
            "application/vnd.ms-excel",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -> {
                intent.component = ComponentName.createRelative(
                    applicationContext,
                    ChooserDocumentActivity::class.java.name
                )
            }
            else -> {
                if (intent.type!!.startsWith("image/")) {
                    intent.component = ComponentName.createRelative(
                        applicationContext,
                        ChooserImagesActivity::class.java.name
                    )
                } else {
                    if (intent.type!!.startsWith("video/")) {
                        intent.component = ComponentName.createRelative(
                            applicationContext,
                            ChooserVideoActivity::class.java.name
                        )
                    } else {
                        finish()
                        return
                    }
                }
            }
        }
        startActivity(intent)
        finish()
    }
}