package com.mints.goodmoney.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.ireader.ireadersdk.IreaderApi
import com.mints.goodmoney.R
import com.mints.goodmoney.common.AppConfig
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.mvp.presenters.BookPresenter
import com.mints.goodmoney.mvp.views.BookView
import com.mints.goodmoney.ui.activitys.AwardActivity
import com.mints.goodmoney.ui.fragment.base.LazyLoadBaseFragment
import com.mints.goodmoney.utils.SpanUtils
import kotlinx.android.synthetic.main.fragment_main_zy.*

/**
 * 描述：掌阅书城
 * 作者：孟崔广
 */
class ZhangyueFragment : LazyLoadBaseFragment(), BookView, IreaderApi.OnReadChangeListener {

    private val bookPresenter by lazy { BookPresenter() }
    private var bookFragment: Fragment? = null

    override fun getContentViewLayoutID() = R.layout.fragment_main_zy

    override fun initViewsAndEvents() {

    }

    override fun onFragmentFirstVisible() {
        bookPresenter.attachView(this)

        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (bookFragment == null) {
            bookFragment = IreaderApi.getNovelBookStoreFragment()
            transaction.add(R.id.fragment, bookFragment!!, bookFragment!!::class.java.simpleName)
            IreaderApi.setReadChangeListener(this)
        } else {
            transaction.show(bookFragment!!)
        }
        transaction.commitAllowingStateLoss()
    }

    override fun onFragmentResume() {
        super.onFragmentResume()
        if (AppConfig.fragmentClickFlag === Constant.FRAGMENT_CLICK_MAIN) {
            bookPresenter.getReadInfo()
        }
    }

    override fun onDestroy() {
        super.onDestroy()

        IreaderApi.setReadChangeListener(null)

        bookPresenter.detachView()
    }

    override fun onReadStart() {
    }

    override fun onReadEnd(time: Long) {
//        LogUtil.d(TAG, "onReadEnd time:" + time)
//        LogUtil.d(TAG, "onReadEnd time(Int):" + (time/1000).toInt())

        val seconds = (time / 1000).toInt()
        // 阅读5秒以上 保存时长
        if (seconds > 5) {
            bookPresenter.addReadSeconds(seconds)
        }
    }

    override fun onReadPage(time: Long) {
    }

    override fun readSecondsSuc(coin: Int) {
        if (coin > 0) {
            val bundle = Bundle()
            bundle.putString(Constant.MAIN_CARRIER_TYPE, Constant.CARRIER_ZHANGYUE)
            bundle.putInt(Constant.MAIN_CUR_COIN, coin)
            readyGo(AwardActivity::class.java, bundle)
        }
    }

    override fun readInfo(sumTime: Int, sumCoin: Int, readTime: Int) {
        //阅读{10}分钟，奖励{xx}金币，已读{x}分钟
        tvZhangContent.visibility = View.VISIBLE
        if (readTime >= sumTime) {
            tvZhangContent.setText("今日阅读任务已完成")
        } else {
            tvZhangContent.setText(
                    SpanUtils()
                            .append("阅读").append("${sumTime / 60}").setForegroundColor(mContext.resources.getColor(R.color.main_mints)).append("分钟, ")
                            .append("奖励").append("${sumCoin}").setForegroundColor(mContext.resources.getColor(R.color.red)).append("金币, ")
                            .append("已读").append("${readTime / 60}").setForegroundColor(mContext.resources.getColor(R.color.main_mints)).append("分钟")
                            .create())
        }
    }
}