package com.khmer9.kdmain.screen.main

import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.khmer9.kdmain.R
import com.khmer9.kdmain.adapter.MainModuleAdapter
import com.khmer9.kdmain.app.App
import com.khmer9.kdmain.app.DownloadController
import com.khmer9.kdmain.app.ShareActivity
import com.khmer9.kdmain.base.BaseFragment
import com.khmer9.kdmain.config.Config
import com.khmer9.kdmain.extension.btnClick
import com.khmer9.kdmain.remote.User
import kotlinx.android.synthetic.main.layout_sub_module.*

class SubModuleFragment : BaseFragment(R.layout.layout_sub_module) {
    private val shareViewModel = App.injectShareViewModel()
    private lateinit var user: User
    private lateinit var adapter: MainModuleAdapter
    private lateinit var recyclerView: RecyclerView
    private lateinit var betTouch: MediaPlayer
    private val ARG_PARAM1 = "module_id"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        (requireActivity() as ShareActivity).apply {
            user = loginData.user!!
            betTouch = MediaPlayer.create(this, R.raw.bettouch)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as ShareActivity).apply {
            recyclerView = view.findViewById(R.id.rv_main)

            close_btn.setOnClickListener {
                it.btnClick().subscribe() {
                    (requireActivity() as? ShareActivity)?.apply {
                        onBackPressed()
                    }
                }
            }

            val moduleId = requireArguments().getString(ARG_PARAM1)
            if (!moduleId.isNullOrEmpty()) {
                shareViewModel.remoteSubModule(moduleId)
                shareViewModel.getSubModuleState.observe(viewLifecycleOwner, Observer {
                    setModuleSub(it)
                })
            }
        }
    }

    private fun setModuleSub(state: ResultSubState) {
        if (state.resultList != null) {
            adapter = MainModuleAdapter()
            adapter.submitList(state.resultList)
            recyclerView.setHasFixedSize(true)
            recyclerView.layoutManager = LinearLayoutManager(context)
            recyclerView.adapter = adapter

            adapter.detailButtonListener = { id, appId, sub ->
                if (!id.isNullOrEmpty()) {
                    Log.d("SubModuleClick", id + "-" + appId + "-" + sub)
                    when (appId) {
                        Config.LOTTO_APP_ID -> {
                            (requireActivity() as ShareActivity).apply {
                                val launchIntent = packageManager.getLaunchIntentForPackage(Config.LOTTO_PACKAGE)
                                if (launchIntent != null) {
                                    shareViewModel.getModuleToken(appId, launchIntent)
                                } else {
                                    shareViewModel.getModuleToken(appId, null)
                                    //doDownload(LOTTO_DOWNLOAD)
                                }
                            }
                        }
                        Config.LOTTO_PLUS_APP_ID -> {
                            (requireActivity() as ShareActivity).apply {
                                val launchIntent = packageManager.getLaunchIntentForPackage(Config.LOTTO_PLUS_PACKAGE)
                                if (launchIntent != null) {
                                    shareViewModel.getModuleToken(appId, launchIntent)
                                } else {
                                    shareViewModel.getModuleToken(appId, null)
                                    //doDownload(LOTTO_PLUS_DOWNLOAD)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private fun doDownload(url: String) {
        try {
            (requireActivity() as ShareActivity).apply {
                downloadController = DownloadController(this, url)
                checkStoragePermission()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}