
export const ProfileFragment = (configuration) => {
    return `
    package ${configuration.packageName}.ui.fragments



    import android.content.Intent
    import android.graphics.Bitmap
    import android.net.Uri
    import android.os.Bundle
    import android.provider.MediaStore
    import android.view.View
    import android.widget.Toast
    import androidx.fragment.app.Fragment
    import androidx.navigation.fragment.findNavController
    import android.content.pm.PackageManager
    import androidx.core.content.ContextCompat
    import ${configuration.name}.R
    import ${configuration.name}.databinding.BottomUpdateProfileBinding
    import ${configuration.name}.databinding.FragmentProfileBinding
    import ${configuration.packageName}.network.Status
    import ${configuration.packageName}.utils.FileUtils.saveImage
    import ${configuration.packageName}.extensions.RequestCodes
    import ${configuration.packageName}.viewmodels.*
    import com.google.android.material.bottomsheet.BottomSheetDialog
    import ${configuration.packageName}.data.local.AppPreferences
    import ${configuration.packageName}.extensions.*
    import okhttp3.MediaType.Companion.toMediaTypeOrNull
    import okhttp3.MultipartBody
    import okhttp3.RequestBody.Companion.asRequestBody
    import org.koin.android.ext.android.inject
    import org.koin.androidx.viewmodel.ext.android.viewModel
    import org.spongycastle.asn1.cms.CMSAttributes.contentType
    import java.io.File
    import java.io.IOException
    
    class ProfileFragment : Fragment(R.layout.fragment_profile) {
    
        private val binding by viewBinding(FragmentProfileBinding::bind)
        private val viewModel: BaasViewModel by viewModel()
        private val pref by inject<AppPreferences>()
    
        @Deprecated("Deprecated in Java")
        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (data != null) {
                if (requestCode == RequestCodes.GALLERY_IMAGE) {
                    val contentURI: Uri? = data.data
                    try {
                        val bitmap =
                            MediaStore.Images.Media.getBitmap(activity?.contentResolver, contentURI)
                        uploadProfileImage(bitmap)
                    } catch (e: IOException) {
                        e.printStackTrace()
                        Toast.makeText(context, "Failed!", Toast.LENGTH_SHORT).show()
                    }
                }
                if (requestCode == RequestCodes.CAMERA_IMAGE) {
                    try {
                        val bitmap = data.extras?.get("data") as Bitmap
                        uploadProfileImage(bitmap)
                    } catch (e: IOException) {
                        e.printStackTrace()
                        Toast.makeText(context, "Failed!", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

            if (ContextCompat.checkSelfPermission(
                requireContext(),
                android.Manifest.permission.CAMERA
                ) != PackageManager.PERMISSION_GRANTED
            ) askForCameraPermission()
    
            binding.textViewFirstName.setText(pref.firstName)
            binding.textViewLastName.setText(pref.lastName)
            binding.textViewEmail.setText(pref.email)



            binding.buttonEditProfile.setOnClickListener {

                findNavController().navigate(R.id.action_profileFragment_to_profileEditFragment)
            }
            
            binding.btnLogout.setOnClickListener {
                Constants.isAuthorized.postValue(false)
            }

            binding.ivUser.loadImageFromUrl(pref.photo)
    
        }
    
        private fun updateProfile(firstName: String, lastName: String, photo: String) {
    
            viewModel.profileUpdate(firstName, lastName, photo ).observe(viewLifecycleOwner) {
                when (it.status) {
                    Status.ERROR -> {
                        snackBar(it.message ?: "Something went wrong")
                    }
                    Status.LOADING -> {}
                    Status.SUCCESS -> {
                        println(it.data)
                        pref.lastName = lastName
                        pref.firstName = firstName
                        snackBar(it.message ?: "Profile Updated")
                    }
                }
            }
        }
    
        private fun uploadProfileImage(image: Bitmap) {
            val file = File(saveImage(image))
            val contentType = "multipart/form-data".toMediaTypeOrNull()
            val part = MultipartBody.Part.createFormData(
                "file", file.name, file.asRequestBody(contentType)
            )
            viewModel.uploadimages3( part ).observe(viewLifecycleOwner) {
                when (it.status) {
                    Status.ERROR -> {
                        snackBar(it.message ?: "Something went wrong")
                    }
                    Status.LOADING -> {}
                    Status.SUCCESS -> {
                        pref.photo = it.data?.url
                        binding.ivUser.loadImageFromUrl(pref.photo)
                    }
                }
            }
        }
    
    
    
    
        private fun showImageSheet() {
            val sheet = BottomSheetDialog(requireContext())
            val view = BottomUpdateProfileBinding.inflate(layoutInflater)
    
            view.imageView6.setOnClickListener {
                sheet.dismiss()
            }
    
            view.btnCamera.setOnClickListener {
                sheet.dismiss()
                startActivityForResult(
                    Intent(MediaStore.ACTION_IMAGE_CAPTURE), RequestCodes.CAMERA_IMAGE
                )
            }
    
            view.btnGallery.setOnClickListener {
                sheet.dismiss()
                val intent = Intent().apply {
                    type = "image/*"
                    action = Intent.ACTION_GET_CONTENT
                }
                startActivityForResult(
                    Intent.createChooser(intent, "Select image"), RequestCodes.GALLERY_IMAGE
                )
            }
    
            sheet.setCancelable(true)
            sheet.setContentView(view.root)
            sheet.show()
        }
    
    }
    `
}