/*
* Created by WebID Solutions GmbH | www.webid-solutions.de.
* See the file "LICENSE" for the full license governing this code.
*/
package de.webidsolutions.metaplugindemo.tasks

import de.webidsolutions.meta_plugin.DuplicationPluginsException
import de.webidsolutions.meta_plugin.WebIdMetaPlugin
import de.webidsolutions.mobile_app.sdk.EidIdentRequiredException
import de.webidsolutions.mobile_app.sdk.IdentNotSupportedException
import de.webidsolutions.mobile_app.sdk.LegalTermsConfirmationMissingException
import de.webidsolutions.mobile_app.sdk.ServiceUnavailableException
import de.webidsolutions.mobile_app.sdk.UserActionNotFinishedException
import de.webidsolutions.mobile_app.sdk.UserActionNotFoundException
import de.webidsolutions.mobile_app.sdk.domain.VerifyActionIdResult
import de.webidsolutions.mobile_app.sdk.impl.AsyncTaskResultGeneric
import de.webidsolutions.mobile_app.sdk.impl.IOnPostExecuteCallback
import de.webidsolutions.mobile_app.sdk.impl.WebIdAsyncTask
import java.io.IOException

internal class MetaPluginVerifyTask(
    private val metaPlugin: WebIdMetaPlugin
) : WebIdAsyncTask<String, Void, AsyncTaskResultGeneric<VerifyActionIdResult?, EApiResult>>() {

    private var metaPluginVerifyOnPostExecuteCallback: IOnPostExecuteCallback<AsyncTaskResultGeneric<VerifyActionIdResult?, EApiResult>>? =
        null

    override fun doInBackground(vararg params: String): AsyncTaskResultGeneric<VerifyActionIdResult?, EApiResult> {
        val apiResult = try {
            return AsyncTaskResultGeneric(metaPlugin.verify(params[0]), EApiResult.SUCCESS)
        } catch (e: DuplicationPluginsException) {
            EApiResult.DUPLICATE_PLUGIN
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
            if (e.message!!.startsWith("actionId")) {
                EApiResult.ACTIONID_NOT_VALID
            } else {
                EApiResult.ILLEGAL_ARGUMENT
            }
        } catch (e: LegalTermsConfirmationMissingException) {
            e.printStackTrace()
            if (e.message!!.startsWith("actionId")) {
                EApiResult.ACTIONID_NOT_VALID
            } else {
                EApiResult.ILLEGAL_ARGUMENT
            }
        } catch (e: UserActionNotFinishedException) {
            e.printStackTrace()
            if (e.message!!.startsWith("actionId")) {
                EApiResult.ACTIONID_NOT_VALID
            } else {
                EApiResult.ILLEGAL_ARGUMENT
            }
        } catch (e: IdentNotSupportedException) {
            e.printStackTrace()
            if (e.message!!.startsWith("actionId")) {
                EApiResult.ACTIONID_NOT_VALID
            } else {
                EApiResult.ILLEGAL_ARGUMENT
            }
        } catch (e: EidIdentRequiredException) {
            e.printStackTrace()
            if (e.message!!.startsWith("actionId")) {
                EApiResult.ACTIONID_NOT_VALID
            } else {
                EApiResult.ILLEGAL_ARGUMENT
            }
        } catch (e: AccessDeniedException) {
            e.printStackTrace()
            EApiResult.ACCESS_DENIED.apply { extInfo = e.message!! }
        } catch (e: UserActionNotFoundException) {
            e.printStackTrace()
            EApiResult.ACTIONID_NOT_FOUND
        } catch (e: IOException) {
            e.printStackTrace()
            EApiResult.IO_FAILURE.apply { extInfo = e.message!! }
        } catch (e: IllegalStateException) {
            e.printStackTrace()
            EApiResult.ILLEGAL_STATE
        } catch (e: ServiceUnavailableException) {
            e.printStackTrace()
            EApiResult.SERVICE_UNAVAILABLE
        }
        return AsyncTaskResultGeneric(null, apiResult)
    }

    override fun onPostExecute(result: AsyncTaskResultGeneric<VerifyActionIdResult?, EApiResult>?) {
        metaPluginVerifyOnPostExecuteCallback?.let { clb -> result?.let { clb.onPostExecute(it) } }
    }

    fun setOnPostExecuteCallback(onPostExecuteCallback: IOnPostExecuteCallback<AsyncTaskResultGeneric<VerifyActionIdResult?, EApiResult>>): MetaPluginVerifyTask {
        metaPluginVerifyOnPostExecuteCallback = onPostExecuteCallback
        return this
    }
}
