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

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import xyz.myachin.saveto.R
import xyz.myachin.saveto.logic.CHO
import xyz.myachin.saveto.logic.file.FileProcessor
import xyz.myachin.saveto.logic.intent.*
import xyz.myachin.saveto.logic.url.UrlProcessor
import xyz.myachin.saveto.ui.sharing.supports.NeedDownloaderActivity

abstract class ChooserBaseActivity : Activity() {
    private lateinit var mFileProcessor: FileProcessor
    private lateinit var mUrlProcessor: UrlProcessor

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (intent == null) {
            finishThisActivity()
            return
        }
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        val ip = IntentProcessor(intent)
        when (ip.getContentTypeForIntent()) {
            ContentType.FILE, ContentType.TEXT, ContentType.BAD_URL -> {
                mFileProcessor = FileProcessor(ip)
                startActivityForResult(ip.getCreateFileIntent(), CREATE_FILE_INTENT_CODE)
            }
            ContentType.GOOD_URL -> {
                mUrlProcessor = UrlProcessor(ip)
                try {
                    startActivity(mUrlProcessor.processGoodUrl())
                    finishThisActivity()
                } catch (_: ActivityNotFoundException) {
                    startActivityForResult(
                        Intent(
                            applicationContext,
                            NeedDownloaderActivity::class.java
                        ).apply {
                            action = ACTION_NEED_DOWNLOAD
                        }, NEED_DOWNLOADER_CODE
                    )
                }
            }

            ContentType.DAMAGED -> {
                Toast.makeText(applicationContext, R.string.damaged_intent, Toast.LENGTH_LONG)
                    .show()
                finishThisActivity()
            }
            ContentType.UNKNOWN -> {
                Toast.makeText(
                    applicationContext,
                    getString(R.string.cannot_process_this, getString(R.string.app_name)),
                    Toast.LENGTH_LONG
                ).show()
                finishThisActivity()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            CREATE_FILE_INTENT_CODE -> {
                if (RESULT_OK != resultCode) {
                    finishThisActivity()
                    return
                }
                mFileProcessor.prepareOutput(data?.data, contentResolver)
                    .let { res ->
                        when (res) {
                            true -> mFileProcessor.writeFile()
                            false -> {
                                Toast.makeText(
                                    applicationContext,
                                    R.string.damaged_intent,
                                    Toast.LENGTH_SHORT
                                ).show()
                                mFileProcessor.closeStreams()
                            }
                        }
                    }
                mFileProcessor.getRemoveFileIntent()?.run {
                    startActivity(this)
                }
            }
            NEED_DOWNLOADER_CODE -> {
                if (RESULT_OK != resultCode) {
                    finishThisActivity()
                    return
                }
                try {
                    startActivity(data)
                } catch (e: ActivityNotFoundException) {
                    Log.e(CHO, "onActivityResult: market not installed", e)
                    Toast.makeText(
                        applicationContext,
                        R.string.cannot_find_market,
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
        finishThisActivity()
    }

    private fun finishThisActivity() {
        finish()
    }
}