package es.verdnatura.presentation.view.feature.login.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.util.Log.d
import androidx.annotation.RequiresApi
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import es.verdnatura.MobileApplication
import es.verdnatura.R
import es.verdnatura.databinding.FragmentLoginBinding
import es.verdnatura.domain.ConstAndValues.ANDROID_ID
import es.verdnatura.domain.ConstAndValues.PASSWORD
import es.verdnatura.domain.ConstAndValues.REMEMBER
import es.verdnatura.domain.ConstAndValues.RENEWINTERVAL
import es.verdnatura.domain.ConstAndValues.RENEWPERIOD
import es.verdnatura.domain.ConstAndValues.TOKEN
import es.verdnatura.domain.ConstAndValues.TOKENCREATED
import es.verdnatura.domain.ConstAndValues.TTL
import es.verdnatura.domain.ConstAndValues.USER
import es.verdnatura.domain.ConstAndValues.WORKFORMSELECTED
import es.verdnatura.domain.notNull
import es.verdnatura.domain.toast
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnTrashItemRowClickListener
import es.verdnatura.presentation.common.OnWorkFormsItemRowClickListener
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.component.CustomDialogInputThreeValues
import es.verdnatura.presentation.view.component.CustomDialogList
import es.verdnatura.presentation.view.feature.login.adapter.WorkFormAdapter
import es.verdnatura.presentation.view.feature.login.model.WorkForms
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import kotlinx.coroutines.runBlocking
import java.io.File
import java.util.*
import kotlin.system.exitProcess

class LoginFragment(var imageUri: Uri?) :
    BaseFragment<FragmentLoginBinding, LoginViewModel>(LoginViewModel::class) {

    private lateinit var customDialog: CustomDialog
    private lateinit var customNewForm: CustomDialogInputThreeValues
    private var workFormAdapter: WorkFormAdapter? = null
    private lateinit var customDialogList: CustomDialogList
    private lateinit var listForms: List<WorkForms>

    companion object {
        fun newInstance(imageUri: Uri?) = LoginFragment(imageUri)
    }

    override fun getLayoutId(): Int = R.layout.fragment_login

    @RequiresApi(Build.VERSION_CODES.O)
    override fun init() {
        customDialogList = CustomDialogList(requireContext())
        customDialog = CustomDialog(requireContext())
        createFolderSerial()
        saveIdentifiers()
        checkUser()
        binding.buttonLogin.setOnClickListener {
            viewModel.loginSalix(
                user = binding.edittextUsername.text.toString(),
                password = binding.edittextPassword.text.toString()

            )
        }

        if (mobileApplication.dataStoreApp.readDataStoreKey<String>(TOKEN)
                .isNotBlank() && binding.switchRemember.isChecked
        ) {
            viewModel.operator_add()
        }

        binding.textviewRememberPassword.setOnClickListener {
            binding.edittextUsername.setText("")
            binding.edittextPassword.setText("")
            binding.switchRemember.isChecked = false
            runBlocking { mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, false) }
        }

        var myWorkFormSlected =
            mobileApplication.dataStoreApp.readDataStoreKey(WORKFORMSELECTED) as String
        if (myWorkFormSlected.isBlank()) {
            runBlocking {
                mobileApplication.dataStoreApp.editDataStoreKey(
                    WORKFORMSELECTED, "Producción"
                )
                mobileApplication.dataStoreApp.saveWorkForm(
                    WorkForms(
                        "Producción",
                        "https://app.verdnatura.es",
                        "https://salix.verdnatura.es"
                    )
                )
            }
        }
        binding.edittextServer.setText(
            mobileApplication.dataStoreApp.readDataStoreKey(WORKFORMSELECTED) as String
        )


        if (binding.edittextServer.text.toString() == "TestLOCAL") {
            binding.edittextUsername.setText("developer")
            binding.edittextPassword.setText("nightmare")
        }
        // se crean/listan las opciones de formas de trabajo
        //deleteWorkForm("all")
        listForms = runBlocking { mobileApplication.dataStoreApp.getWorkForm() }
        binding.edittextServer.keyListener = null
        binding.edittextServer.setOnLongClickListener {
            showWorkingForms()
            true
        }
        binding.edittextUsername.requestFocus()

    }

    private fun createFolderSerial() {

        val directory = File(
            requireContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "/dataSerial"
        )

        if (!directory.exists()) {
            directory.mkdirs()
        }
        try {
            val filename = "serial.txt"
            val file = File(directory, filename)
            mobileApplication.serialNumber = file.readText()
        } catch (ex: Exception) {
            d("VERDNATURA:", getString(R.string.errorFile))
        }

    }

    private fun setAdapter(listWorkForms: List<WorkForms>) {

        workFormAdapter = WorkFormAdapter(listWorkForms, object : OnWorkFormsItemRowClickListener {

            override fun OnWorkFormsItemRowClickListener(item: WorkForms) {

                when (item.name) {
                    getString(R.string.add) -> {
                        showNewForm()
                    }

                    else -> {
                        binding.edittextServer.setText(item.name)
                        runBlocking {
                            mobileApplication.dataStoreApp.editDataStoreKey(
                                "base_url", item.urlSilex
                            )
                            mobileApplication.dataStoreApp.editDataStoreKey(
                                "base_urlSalix", item.urlSalix
                            )
                        }

                        runBlocking {
                            mobileApplication.dataStoreApp.editDataStoreKey(
                                WORKFORMSELECTED, item.name
                            )
                        }
                        customDialogList.dismiss()
                        exitProcess(1)

                    }
                }

            }
        }, object : OnTrashItemRowClickListener {
            override fun OnTrashItemRowClickListener(workForm: WorkForms) {

                customDialog.setTitle(getString(R.string.deleteWorkForm))
                    .setDescription(getString(R.string.deleteWorkFormConfirmation))
                    .setOkButton(getString(R.string.cancel)) {
                        customDialog.dismiss()
                    }.setOkButton(getString(R.string.ok)) {

                        val workFormNameKey = stringPreferencesKey(workForm.name)

                        runBlocking {
                            mobileApplication.dataStoreApp.deleteWorkForm(
                                workFormNameKey
                            )
                        }
                        //  deleteWorkForm(workForm.name)
                        workFormAdapter!!.notifyDataSetChanged()
                        customDialog.dismiss()
                        customDialogList.dismiss()
                        exitProcess(1)

                    }.show()

            }
        })


        customDialogList.getRecyclerView().adapter = workFormAdapter
        customDialogList.getRecyclerView().layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)


        customDialogList.setTitle(getString(R.string.selectWorkingForm)).hideDescription()
            .hideDialog().setOkButton(getString(R.string.cancel)) {
                customDialogList.dismiss()
            }.show()

    }

    @SuppressLint("HardwareIds")
    private fun saveIdentifiers() {

        val androidId: String =
            Settings.Secure.getString(requireActivity().contentResolver, Settings.Secure.ANDROID_ID)
        mobileApplication.androidId = androidId

        runBlocking {
            mobileApplication.dataStoreApp.editDataStoreKey(ANDROID_ID, androidId)
        }

    }

    private fun showWorkingForms() {

        var listForms = runBlocking { mobileApplication.dataStoreApp.getWorkForm() }
        setAdapter(listForms)
    }

    private fun showNewForm() {
        customNewForm =
            CustomDialogInputThreeValues(requireContext()).setTitle(getString(R.string.workFormAdd))
                .setDescription(getString(R.string.workFormDescrip))
        customNewForm.setInfoTex(
            getString(R.string.name),
            getString(R.string.silexServer),
            getString(R.string.salixServer)
        )
        customNewForm.setFocusText()
        customNewForm.setOkButton(getString(R.string.save)) {
            runBlocking {
                mobileApplication.dataStoreApp.saveWorkForm(
                    WorkForms(
                        customNewForm.getValue(),
                        customNewForm.getValueTwo(),
                        customNewForm.getValueThree(),
                        true
                    )
                )
            }

            binding.edittextServer.setText(customNewForm.getValue())
            runBlocking {
                mobileApplication.dataStoreApp.editDataStoreKey(
                    WORKFORMSELECTED, customNewForm.getValue()
                )
            }
            customNewForm.dismiss()
            customDialogList.dismiss()
            exitProcess(1)

        }.setKoButton(getString(R.string.cancel)) {
            customNewForm.dismiss()
        }

        customNewForm.show()

    }

    private fun checkUser() {

        if (mobileApplication.dataStoreApp.readDataStoreKey(REMEMBER)) {

            binding.edittextUsername.setText(
                mobileApplication.dataStoreApp.readDataStoreKey<String>(
                    USER
                )
            )
            binding.edittextPassword.setText(
                mobileApplication.dataStoreApp.readDataStoreKey<String>(
                    PASSWORD
                )
            )
            if (mobileApplication.dataStoreApp.readDataStoreKey<String>(TOKEN) != "") {
                binding.switchRemember.isChecked = true
            }
        }

    }

    override fun observeViewModel() {
        with(viewModel) {

            versionappitem.observe(viewLifecycleOwner) {

                if (it.isError) {
                    customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()
                } else {

                    if (it.version == getInfoVersionNameApp()) {
                        goToMain()
                    } else {

                        customDialog.setTitle(getString(R.string.Actualizar))
                            .setDescription(getString(R.string.updatemng)).setOkButton(
                                getString(
                                    R.string.Actualizar
                                )
                            ) {
                                try {
                                    val openURL = Intent(Intent.ACTION_VIEW)
                                    openURL.data = Uri.parse(it.url)
                                    startActivity(openURL)
                                } catch (ex: Exception) {
                                    getString(R.string.problemUpdate).toast(requireActivity())
                                }

                            }
                        if (it.IsVersionCritical == 1) {
                            customDialog.setDescription(getString(R.string.updatecritical))
                        } else {
                            customDialog.setKoButton(getString(R.string.cancel)) {
                                customDialog.dismiss()
                                goToMain()
                            }
                        }
                        customDialog.show()

                    }

                }

            }
            logindevice.observe(viewLifecycleOwner) {

                if (it.isError) {
                    customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()
                } else {
                    if (it.vIsAuthorized == "0") {

                        customDialog.setTitle(getString(R.string.error))
                            .setDescription(it.vMessage!!).setOkButton(getString(R.string.close)) {
                                customDialog.dismiss()
                            }.show()

                    } else {

                        runBlocking {
                            if (binding.switchRemember.isChecked) {
                                mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, true)
                                //saveRemember(true)
                            } else {
                                mobileApplication.dataStoreApp.editDataStoreKey(REMEMBER, false)
                                //saveRemember(false)
                            }
                        }
                        if (it.vIsAuthorized == "1") {

                            if (it.vMessage.isNullOrEmpty()) {
                                getVersion()
                            } else {
                                customDialog.setTitle(getString(R.string.info))
                                    .setDescription(it.vMessage)
                                    .setOkButton(getString(R.string.close)) {
                                        customDialog.dismiss()
                                        getVersion()
                                    }.show()
                            }

                        }

                    }

                }

            }

            workerOperator.observe(viewLifecycleOwner) {
                if (it.isError) {
                    customDialog.setTitle(getString(R.string.info)).setDescription(it.errorMessage)
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()
                } else {

                    runBlocking {
                        mobileApplication.dataStoreApp.saveDataOperator(
                            it
                        )
                    }

                    //saveDataOperator(it)

                }
            }
            operatorAdd.observe(viewLifecycleOwner) {

                if (it.isError) {
                    customDialog.setTitle(getString(R.string.info)).setDescription(it.errorMessage)
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()
                }

                viewModel.getCurrentUserData(
                    getString(R.string.logAppName),
                    getInfoVersionNameApp(),
                    mobileApplication.dataStoreApp.readDataStoreKey(ANDROID_ID)
                )
                //  viewModel.operator_getNumberOfWagons()
            }

            devicelogresponse.observe(viewLifecycleOwner) {

                if (it.isError) {
                    customDialog.setTitle(getString(R.string.error)).setDescription(it.errorMessage)
                        .setOkButton(
                            getString(R.string.accept)
                        ) {
                            customDialog.dismiss()
                            getVersion()
                        }.show()
                }
            }


            loginSalixItem.observe(viewLifecycleOwner) {

                if (it.isError) {
                    saveTokenPref("", 0)
                    customDialog.setTitle(getString(R.string.error))

                        .setDescription(
                            if (it.errorMessage.contains("Unauthorized")) {
                                getString(R.string.messageUserError)
                            } else {
                                it.errorMessage
                            }
                        ).setOkButton(
                            getString(R.string.accept)
                        ) {
                            customDialog.dismiss()

                        }.show()
                } else {
                    saveTokenPref(it.token, it.ttl)
                    //test
                    //saveTokenPref("RTymwJzRuVchAMY5u8xG2wpgcjPf7t0DQImLvMsTdNFmbFwDiVsa8lA6fr1XSaBo","2026-29-06")
                    // local
                    //saveTokenPref("1ZGV1OGhpcrZZwvNALXnPeSVe0GCE4tgpKbv8nZu4EXay75QZrVY5GZDnpsqlxFz",it.ttl )

                    app.userName = binding.edittextUsername.text.toString()
                    app.userPassword = binding.edittextPassword.text.toString()

                    viewModel.getAccessTokenConfigs()
                }
            }
            loadAccessConfigSalixList.observe(viewLifecycleOwner, Observer { event ->

                event.getContentIfNotHandled().notNull {

                    if (it.list.isEmpty()) {
                        getString(R.string.errorConfigToken).toast(context)

                    } else {
                        if (!it.list[0].isError) {
                            runBlocking {
                                mobileApplication.dataStoreApp.editDataStoreKey(
                                    RENEWPERIOD, it.list[0].renewPeriod
                                )
                                mobileApplication.dataStoreApp.editDataStoreKey(
                                    RENEWINTERVAL, it.list[0].renewInterval
                                )
                            }

                            viewModel.operator_add()

                        } else {
                            it.list[0].errorMessage.toast(context)
                        }
                    }
                }

            })

        }
        super.observeViewModel()
    }

    private fun setupFirebase(userName: String) {
        Firebase.crashlytics.setUserId(userName)
    }

    private fun getInfoVersionNameApp(): String {
        val manager: PackageManager = requireContext().packageManager
        val info: PackageInfo = manager.getPackageInfo(requireContext().packageName, 0)
        mobileApplication.versionName = info.versionName
        return info.versionName
    }

    private fun saveTokenPref(token: String, ttl: Long) {

        runBlocking {
            mobileApplication.dataStoreApp.editDataStoreKey(TOKEN, token)
            mobileApplication.dataStoreApp.editDataStoreKey(TTL, ttl)
            mobileApplication.dataStoreApp.editDataStoreKey(TOKENCREATED, Date().time)
        }

    }

    private fun goToMain() {
        setupFirebase((requireActivity().application as MobileApplication).userName.toString())
        val intent = Intent(activity, MainActivity::class.java)
        intent.data = imageUri
        startActivity(intent)
        requireActivity().finish()
    }

    private fun getVersion() {
        viewModel.checkVersion(
            nameApp = getString(R.string.nameApp)

        )

    }

}

