<commit_before>package com.sedsoftware.yaptalker.presentation.features.posting

import com.arellomobile.mvp.InjectViewState
import com.sedsoftware.yaptalker.domain.interactor.topic.GetEmojiList
import com.sedsoftware.yaptalker.presentation.base.BasePresenter
import com.sedsoftware.yaptalker.presentation.base.enums.lifecycle.PresenterLifecycle
import com.sedsoftware.yaptalker.presentation.base.enums.navigation.RequestCode
import com.sedsoftware.yaptalker.presentation.features.posting.MessageTagCodes.Tag
import com.sedsoftware.yaptalker.presentation.mappers.EmojiModelMapper
import com.sedsoftware.yaptalker.presentation.model.YapEntity
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import ru.terrakok.cicerone.Router
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject

@InjectViewState
class AddMessagePresenter @Inject constructor(
  private val router: Router,
  private val getEmojiListUseCase: GetEmojiList,
  private val emojiMapper: EmojiModelMapper
) : BasePresenter<AddMessageView>() {

  private var clearCurrentList = false

  private var isBOpened = false
  private var isIOpened = false
  private var isUOpened = false

  override fun onFirstViewAttach() {
    super.onFirstViewAttach()

    loadEmojiList()
  }

  override fun attachView(view: AddMessageView?) {
    super.attachView(view)
    viewState.updateCurrentUiState()
  }

  override fun detachView(view: AddMessageView?) {
    viewState.hideKeyboard()
    super.detachView(view)
  }

  fun insertChosenTag(selectionStart: Int, selectionEnd: Int, @Tag tag: Long) {
    when {
      tag == MessageTagCodes.TAG_LINK -> onLinkTagClicked()
      tag == MessageTagCodes.TAG_VIDEO -> onVideoLinkTagClicked()
      selectionStart != selectionEnd -> onTagClickedWithSelection(tag)
      else -> onTagClickedWithNoSelection(tag)
    }
  }

  fun insertLinkTag(url: String, title: String) {
    val result = String.format(Locale.getDefault(), MessageTags.LINK_BLOCK, url, title)
    viewState.insertTag(result)
  }

  fun insertVideoTag(url: String) {
    val result = String.format(Locale.getDefault(), MessageTags.VIDEO_BLOCK, url)
    viewState.insertTag(result)
  }

  fun insertEmoji(code: String) {
    viewState.insertTag(code)
  }

  fun sendMessageTextBackToView(message: String, isEdited: Boolean) {
    if (isEdited) {
      router.exitWithResult(RequestCode.EDITED_MESSAGE_TEXT, message)
    } else {
      router.exitWithResult(RequestCode.MESSAGE_TEXT, message)
    }
  }

  private fun onTagClickedWithSelection(@Tag tag: Long) {
    when (tag) {
      MessageTagCodes.TAG_B -> {
        viewState.insertTags(MessageTags.B_OPEN, MessageTags.B_CLOSE)
      }
      MessageTagCodes.TAG_I -> {
        viewState.insertTags(MessageTags.I_OPEN, MessageTags.I_CLOSE)
      }
      MessageTagCodes.TAG_U -> {<commit_msg>Add spaces to emoji code<commit_after>package com.sedsoftware.yaptalker.presentation.features.posting

import com.arellomobile.mvp.InjectViewState
import com.sedsoftware.yaptalker.domain.interactor.topic.GetEmojiList
import com.sedsoftware.yaptalker.presentation.base.BasePresenter
import com.sedsoftware.yaptalker.presentation.base.enums.lifecycle.PresenterLifecycle
import com.sedsoftware.yaptalker.presentation.base.enums.navigation.RequestCode
import com.sedsoftware.yaptalker.presentation.features.posting.MessageTagCodes.Tag
import com.sedsoftware.yaptalker.presentation.mappers.EmojiModelMapper
import com.sedsoftware.yaptalker.presentation.model.YapEntity
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import ru.terrakok.cicerone.Router
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject

@InjectViewState
class AddMessagePresenter @Inject constructor(
  private val router: Router,
  private val getEmojiListUseCase: GetEmojiList,
  private val emojiMapper: EmojiModelMapper
) : BasePresenter<AddMessageView>() {

  private var clearCurrentList = false

  private var isBOpened = false
  private var isIOpened = false
  private var isUOpened = false

  override fun onFirstViewAttach() {
    super.onFirstViewAttach()

    loadEmojiList()
  }

  override fun attachView(view: AddMessageView?) {
    super.attachView(view)
    viewState.updateCurrentUiState()
  }

  override fun detachView(view: AddMessageView?) {
    viewState.hideKeyboard()
    super.detachView(view)
  }

  fun insertChosenTag(selectionStart: Int, selectionEnd: Int, @Tag tag: Long) {
    when {
      tag == MessageTagCodes.TAG_LINK -> onLinkTagClicked()
      tag == MessageTagCodes.TAG_VIDEO -> onVideoLinkTagClicked()
      selectionStart != selectionEnd -> onTagClickedWithSelection(tag)
      else -> onTagClickedWithNoSelection(tag)
    }
  }

  fun insertLinkTag(url: String, title: String) {
    val result = String.format(Locale.getDefault(), MessageTags.LINK_BLOCK, url, title)
    viewState.insertTag(result)
  }

  fun insertVideoTag(url: String) {
    val result = String.format(Locale.getDefault(), MessageTags.VIDEO_BLOCK, url)
    viewState.insertTag(result)
  }

  fun insertEmoji(code: String) {
    viewState.insertTag(" $code ")
  }

  fun sendMessageTextBackToView(message: String, isEdited: Boolean) {
    if (isEdited) {
      router.exitWithResult(RequestCode.EDITED_MESSAGE_TEXT, message)
    } else {
      router.exitWithResult(RequestCode.MESSAGE_TEXT, message)
    }
  }

  private fun onTagClickedWithSelection(@Tag tag: Long) {
    when (tag) {
      MessageTagCodes.TAG_B -> {
        viewState.insertTags(MessageTags.B_OPEN, MessageTags.B_CLOSE)
      }
      MessageTagCodes.TAG_I -> {
        viewState.insertTags(MessageTags.I_OPEN, MessageTags.I_CLOSE)
      }
      MessageTagCodes.TAG_U -> {
{'fraction_non_alphanumeric': 0.09043624161073825, 'fraction_numerical': 0.0, 'mean_word_length': 4.650236966824645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6821497', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1881, 'n_words': 317}