package xyz.myachin.saveto.ui

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

class ChooserActivity : Activity() {
    private lateinit var mFp: FileProcessor
    private lateinit var mUp: 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 -> {
                mFp = FileProcessor(ip)
                startActivityForResult(ip.getCreateFileIntent(), CREATE_FILE_INTENT)
            }
            ContentType.GOOD_URL -> {
                mUp = UrlProcessor(ip)
                try {
                    startActivity(mUp.processGoodUrl())
                    finishThisActivity()
                } catch (_: ActivityNotFoundException) {
                    startActivityForResult(
                        Intent(
                            applicationContext,
                            NeedDownloaderActivity::class.java
                        ).apply {
                            action =
                                ACTION_NEED_DOWNLOAD
                        }, NEED_DOWNLOADER
                    )
                }
            }
//            ContentType.BAD_URL -> {
//                val up = UrlProcessor(ip)
//                up.processBadUrl()
//            }
            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 -> {
                if (RESULT_OK != resultCode) {
                    finishThisActivity()
                    return
                }
                mFp.prepareOutput(data, contentResolver)
                    .let { res ->
                        when (res) {
                            true -> mFp.writeFile()
                            false -> {
                                Toast.makeText(
                                    applicationContext,
                                    R.string.damaged_intent,
                                    Toast.LENGTH_SHORT
                                ).show()
                                mFp.closeStreams()
                            }
                        }
                    }
                mFp.getRemoveFileIntent()?.run {
                    startActivity(this)
                }
            }
            NEED_DOWNLOADER -> {
                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()
    }
}