package com.fastaccess.ui.modules.main.notifications

import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.view.View
import butterknife.BindView
import butterknife.OnClick
import com.fastaccess.R
import com.fastaccess.data.dao.model.AbstractFastHubNotification.NotificationType
import com.fastaccess.data.dao.model.FastHubNotification
import com.fastaccess.helper.BundleConstant
import com.fastaccess.helper.Bundler
import com.fastaccess.helper.PrefGetter
import com.fastaccess.ui.base.BaseDialogFragment
import com.fastaccess.ui.base.mvp.BaseMvp
import com.fastaccess.ui.base.mvp.presenter.BasePresenter
import com.fastaccess.ui.widgets.FontTextView
import com.prettifier.pretty.PrettifyWebView

/**
 * Created by Kosh on 17.11.17.
 */
class FastHubNotificationDialog : BaseDialogFragment<BaseMvp.FAView, BasePresenter<BaseMvp.FAView>>() {

    init {
        suppressAnimation = true
        isCancelable = false
    }

    @BindView(R.id.title) @JvmField var title: FontTextView? = null
    @BindView(R.id.webView) @JvmField var webView: PrettifyWebView? = null
    private val model by lazy { arguments?.getParcelable<FastHubNotification>(BundleConstant.ITEM) }

    @OnClick(R.id.cancel) fun onCancel() {
        dismiss()
    }

    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
        model?.let {
            title?.text = it.title
            webView?.setGithubContent(it.body, null, false, false)
            it.isRead = true
            FastHubNotification.update(it)
        } ?: dismiss()
    }

    override fun providePresenter(): BasePresenter<BaseMvp.FAView> = BasePresenter()

    override fun fragmentLayout(): Int = R.layout.dialog_guide_layout

    companion object {
        @JvmStatic private val TAG = FastHubNotificationDialog::class.java.simpleName

        fun newInstance(model: FastHubNotification): FastHubNotificationDialog {
            val fragment = FastHubNotificationDialog()
            fragment.arguments = Bundler.start()
                    .put(BundleConstant.ITEM, model)
                    .end()
            return fragment
        }

        fun show(fragmentManager: FragmentManager, model: FastHubNotification? = null) {
            val notification = model ?: FastHubNotification.getLatest()
            notification?.let {
                if (it.type == NotificationType.PROMOTION || it.type == NotificationType.PURCHASE && model == null) {
                    if (PrefGetter.isProEnabled()) {
                        it.isRead = true
                        FastHubNotification.update(it)
                        return
                    }
                }
                newInstance(it).show(fragmentManager, TAG)
            }
        }

        fun show(fragmentManager: FragmentManager) {
            show(fragmentManager, null)
        }
    }
}