package com.doverfuelingsolutions.issp.view

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.api.SystemApi
import com.doverfuelingsolutions.issp.api.FuelInfoApi
import com.doverfuelingsolutions.issp.utils.sp.SPKeys
import com.doverfuelingsolutions.issp.utils.sp.SPUtil
import com.doverfuelingsolutions.issp.view.fragment.FragmentPreference

class PreferenceActivity : AppCompatActivity() {

    companion object {

        const val codeRequestResult = 1001
        const val isMiddleModified = "isMiddleModified"
        const val isFuelModified = "isFuelModified"

        fun start(context: Context) {
            Intent(context, PreferenceActivity::class.java).let { context.startActivity(it) }
        }

        fun startForResult(activity: Activity) {
            Intent(activity, PreferenceActivity::class.java).let {
                it.putExtra("forResult", true)
                activity.startActivityForResult(it, codeRequestResult)
            }
        }
    }

    private var isForResult = false
    private var oldMiddleInfo = ""
    private var oldFuelInfo = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_preference)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.container, FragmentPreference())
                .commit()
        }

        val forResult = intent.getBooleanExtra("forResult", false)
        if (forResult) {
            isForResult = true
            oldMiddleInfo = makeMiddleInfo()
            oldFuelInfo = makeFuelInfo()
        } else {
            isForResult = false
        }
    }

    override fun onBackPressed() {
        val hasMiddleModified = makeMiddleInfo() != oldMiddleInfo
        val hasFuelModified = makeFuelInfo() != oldFuelInfo
        if (hasMiddleModified) SystemApi.regenerateAllService()
        if (hasFuelModified) FuelInfoApi.generateService()
        if (isForResult) {
            setResult(Activity.RESULT_OK, Intent().apply {
                putExtra(isMiddleModified, hasMiddleModified)
                putExtra(isFuelModified, hasFuelModified)
            })
            finish()
        } else {
            super.onBackPressed()
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    private fun makeMiddleInfo(): String {
        return "${SPUtil.getString(SPKeys.MIDDLE_IP)}, ${SPUtil.getString(SPKeys.MIDDLE_PORT)}, ${SPUtil.getString(SPKeys.MIDDLE_WORKSTATION_ID)}"
    }

    private fun makeFuelInfo(): String {
        return "${SPUtil.getString(SPKeys.FUEL_IP)}, ${SPUtil.getString(SPKeys.FUEL_PORT)}"
    }
}