package com.khmer9.sbcmain.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.sbcmain.R
import com.khmer9.sbcmain.adapter.MainModuleAdapter
import com.khmer9.sbcmain.app.App
import com.khmer9.sbcmain.app.DownloadController
import com.khmer9.sbcmain.app.ShareActivity
import com.khmer9.sbcmain.base.BaseFragment
import com.khmer9.sbcmain.remote.User

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"

    private val LOTTO_APP_ID = "630780f0f635dd035329f5b7"
    private val LOTTO_DOWNLOAD = ""
    private val LOTTO_PACKAGE = "com.khmer9.lotto"

    private val LOTTO_PLUS_APP_ID = "62e397edae71cfa8782aa3a0"
    private val LOTTO_PLUS_DOWNLOAD = ""
    private val LOTTO_PLUS_PACKAGE = "com.sambath.sbc"

    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)

            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) {
                        LOTTO_APP_ID -> {
                            (requireActivity() as ShareActivity).apply {
                                val launchIntent = packageManager.getLaunchIntentForPackage(LOTTO_PACKAGE)
                                if (launchIntent != null) {
                                    shareViewModel.getModuleToken(appId, launchIntent)
                                } else {
                                    doDownload(LOTTO_DOWNLOAD)
                                }
                            }
                        }
                        LOTTO_PLUS_APP_ID -> {
                            (requireActivity() as ShareActivity).apply {
                                val launchIntent = packageManager.getLaunchIntentForPackage(LOTTO_PLUS_PACKAGE)
                                if (launchIntent != null) {
                                    shareViewModel.getModuleToken(appId, launchIntent)
                                } else {
                                    doDownload(LOTTO_PLUS_DOWNLOAD)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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

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