package xyz.myachin.saveto.ui.share.fake

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import xyz.myachin.saveto.settings.Settings
import xyz.myachin.saveto.ui.notification.ResetDefaultsNotification
import xyz.myachin.saveto.ui.share.direct.DirectShareActivity
import xyz.myachin.saveto.ui.share.manual.ManualShareActivity

class ActionViewActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Thread {
            if (packageManager
                    .resolveActivity(Intent(Intent.ACTION_VIEW).apply {
                        type = intent.type
                    },
                        PackageManager.MATCH_DEFAULT_ONLY)?.activityInfo?.packageName == packageName &&
                Settings.preventBeDefault
            ) {
                ResetDefaultsNotification(applicationContext).reset()
            }
        }.start()

        startActivity(intent.apply {
            action = Intent.ACTION_SEND
            setClassName(packageName,
                if (Settings.actionViewManual) ManualShareActivity::class.java.name else DirectShareActivity::class.java.name)
            putExtra(Intent.EXTRA_STREAM, data)
        })
        finish()
    }
}