package com.hanwha.morakmorak.ui.main.home.detail.info

import android.os.Bundle
import android.view.LayoutInflater
import androidx.lifecycle.Observer
import com.hanwha.morakmorak.R
import com.hanwha.morakmorak.databinding.FragmentDetailInfoBinding
import com.hanwha.morakmorak.ui.BindingFragment
import com.hanwha.morakmorak.ui.main.home.HomeFragment
import com.hanwha.morakmorak.ui.main.home.detail.HomeDetailActivity
import kotlinx.android.synthetic.main.fragment_detail_info.*
import org.koin.android.viewmodel.ext.android.getViewModel

class DetailInfoFragment : BindingFragment<FragmentDetailInfoBinding>() {

    override fun getLayoutResId() = R.layout.fragment_detail_info

    companion object {
        fun newInstance() = DetailInfoFragment()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        binding.run {
            lifecycleOwner = this@DetailInfoFragment
            vm = getViewModel()
        }
//        makeSampleMember()
    }

    override fun onStart() {
        super.onStart()

        (activity as? HomeDetailActivity)?.run {
            setJoinStatusObserver(binding.vm!!.joinStatus)
            getUpdateObserver().observe(this@DetailInfoFragment, Observer { update ->
                if (update) {
                    binding.vm!!.requestPartiesDetail(activity!!.intent.getIntExtra(HomeFragment.EXTRA_PARTY_ID, 0))
                    getUpdateObserver().value = false
                }
            })
        }

        binding.vm!!.requestPartiesDetail(activity!!.intent.getIntExtra(HomeFragment.EXTRA_PARTY_ID, 0))
    }

    private fun makeSampleMember() {
        for (i in 0 until 4) {
            val view = LayoutInflater.from(context).inflate(R.layout.view_home_detail_member, null)
            member_linearLayout.addView(view)
        }
    }

}
