package com.mints.goodmoney.ui.fragment

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.mints.goodmoney.R
import com.mints.goodmoney.common.AppConfig
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.mvp.model.FriendHallMsgBean
import com.mints.goodmoney.mvp.presenters.FriendsPresenter
import com.mints.goodmoney.mvp.views.FriendsView
import com.mints.goodmoney.ui.adapter.FriendsPagerAdapter
import com.mints.goodmoney.ui.adapter.InvitedAdapter
import com.mints.goodmoney.ui.fragment.base.BaseFragment
import com.mints.goodmoney.utils.SpanUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.fragment_main_friends.*
import kotlinx.android.synthetic.main.header_layout.*

/**
 * 描述：邀请好友
 * 作者：孟崔广
 * 时间：2020/9/20 17:30
 */
class FriendsFragment : BaseFragment(), FriendsView, OnRefreshListener, View.OnClickListener {

    private val friendsPresenter by lazy { FriendsPresenter() }

    private val userManager by lazy { UserManager.getInstance() }

    private var viewPagerAdapter: FriendsPagerAdapter? = null

    private var friendsData: MutableList<FriendHallMsgBean.ListBean> = mutableListOf()
    private var worldData: MutableList<FriendHallMsgBean.ListBean> = mutableListOf()

    private val recyList: MutableList<RecyclerView> = mutableListOf()

    private var friendsAdapter: InvitedAdapter? = null
    private var worldAdapter: InvitedAdapter? = null

    private var mFriendsHallMsgBean: FriendHallMsgBean? = null

    override fun getContentViewLayoutID() = R.layout.fragment_main_friends

    override fun initViewsAndEvents() {
        friendsPresenter.attachView(this)
        srl_my.setOnRefreshListener(this)

        initView()
        initRecy()

        friendsPresenter.getFriendHallMsg()
    }

    private fun initView() {
        tv_right_subtitle.visibility = View.VISIBLE
        tv_right_subtitle.text = "玩法规则"
        tv_right_subtitle.setTextColor(resources.getColor(R.color.main_bg_text))
        tv_right_subtitle.setOnClickListener { }
        tv_title.text = "分红福利"

        iv_question.setOnClickListener(this)
        btn_get_bonus.setOnClickListener(this)
        tv_right_subtitle.setOnClickListener(this)
    }

    private fun initRecy() {
        val recyFriends = View.inflate(mContext, R.layout.item_friends_recy, null) as RecyclerView
        val recyWorld = View.inflate(mContext, R.layout.item_friends_recy, null) as RecyclerView
        recyFriends.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL))
        recyWorld.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL))

        friendsAdapter = InvitedAdapter(mContext, friendsData)
        worldAdapter = InvitedAdapter(mContext, worldData)

        recyFriends.adapter = friendsAdapter
        recyWorld.adapter = worldAdapter

        recyList.add(recyFriends)
        recyList.add(recyWorld)

        viewPagerAdapter = FriendsPagerAdapter(recyList)
        view_pager_friends.adapter = viewPagerAdapter
        view_pager_friends.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 1) {
                    friendsPresenter.getContributionLeaders()
                } else {
                    friendsPresenter.getFriendHallMsg()
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })

        tab_friends.setupWithViewPager(view_pager_friends)

    }

    override fun onHiddenChanged(hidden: Boolean) {
        if (hidden) { // 不在最前端界面显示
            onPause()
        } else { // 重新显示到最前端中
            onResume()
        }
        super.onHiddenChanged(hidden)
    }

    override fun onResume() {
        super.onResume()
        if (AppConfig.fragmentClickFlag == Constant.FRAGMENT_CLICK_FRIENDS) {
            if (!TextUtils.isEmpty(userManager?.userID)) {
                if (view_pager_friends.currentItem == 0) {
                    friendsPresenter.getFriendHallMsg()
                } else {
                    friendsPresenter.getContributionLeaders()
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        friendsPresenter.detachView()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        if (userManager.userID.isNotEmpty()) {
            if (view_pager_friends.currentItem == 0) {
                friendsPresenter.getFriendHallMsg()
            } else {
                friendsPresenter.getContributionLeaders()
            }
//            view_pager_friends.currentItem = 0

        }
    }

    private fun generateData(data: FriendHallMsgBean) {
        if (TextUtils.isEmpty(data.button)) {
            btn_get_bonus.text = "结算中"
        }

        btn_get_bonus.isEnabled = data.status == 0 && data.cash.toInt() > 0

        tv_bonus_date.text = data.periods

        tv_bonus_sum.text = SpanUtils()
                .append("预计总分红 ").setForegroundColor(resources.getColor(R.color.black))
                .append(data.titleMsg).setFontSize(52).setForegroundColor(resources.getColor(R.color.color_E72C2B))
                .append(" 元").setForegroundColor(resources.getColor(R.color.black))
                .create()

        tv_yesterday_bonus.text = SpanUtils()
                .append(data.contribution_yesterday.toString()).setForegroundColor(resources.getColor(R.color.color_FF7C00))
                .create()

        tv_today_bonus.text = SpanUtils()
                .append(data.contribution_today.toString()).setForegroundColor(resources.getColor(R.color.color_E72C2B))
                .create()

        tv_bonus_money.text = SpanUtils()
                .append(data.cash.toString()).setFontSize(50).setForegroundColor(resources.getColor(R.color.color_E72C2B))
                .append("元").setFontSize(30).setForegroundColor(resources.getColor(R.color.color_E72C2B))
                .create()

        tv_bonus_rate.text = SpanUtils()
                .append("昨日汇率：").setForegroundColor(resources.getColor(R.color.black))
                .append(data.rate).setForegroundColor(resources.getColor(R.color.color_E72C2B))
                .create()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_question -> {
                // 收益提示

            }
            R.id.tv_right_subtitle -> {
                // 玩法规则

            }
            R.id.btn_get_bonus -> {
                // 领取收益
                mFriendsHallMsgBean?.let {
                    if (it.cash.toInt() > 0 && it.status == 0) {
                        // 去提现
                        friendsPresenter.getContributionOutToCash()
                    } else {
                        showToast("您的贡献值未达到提现要求")
                    }
                }

            }
        }
    }

    override fun getFriendsHallMsgSuc(data: FriendHallMsgBean) {
        srl_my.finishRefresh(true)

        mFriendsHallMsgBean = data

        friendsData.clear()
        friendsData.addAll(data.list)
        friendsAdapter?.notifyDataSetChanged()

        generateData(data)
    }

    override fun getContributionLeadersSuc(data: FriendHallMsgBean) {
        srl_my.finishRefresh(true)

        worldData.clear()
        worldData.addAll(data.list)
        worldAdapter?.notifyDataSetChanged()
    }

    // 提现成功
    override fun getContributionOutToCashSuc() {
        showToast("提现成功")
    }

}