package com.yidian.shenghuoquan.bsetting.ui.setting

import androidx.lifecycle.MutableLiveData
import com.orhanobut.hawk.Hawk
import com.yidian.bcommon.constant.AppConfig
import com.yidian.bcommon.BuildConfig
import com.yidian.bcommon.constant.HawkConfig
import com.yidian.bcommon.http.CommonDataSource
import com.yidian.bcommon.http.bean.CommitFeedbackBean
import com.yidian.bcommon.http.callback.ICommitFeedbackCallback
import com.yidian.bcommon.mvvm.BaseViewModel

/**
 * author: yinjiacheng
 * date: 7/16/21 10:27 PM
 * description: 用户反馈
 */
class FeedbackViewModel : BaseViewModel(), ICommitFeedbackCallback {

  /**
   * 用户输入的反馈字数
   */
  val feedbackWordCount by lazy { MutableLiveData(0) }

  /**
   * 用户输入的反馈内容
   */
  val feedbackContent by lazy { MutableLiveData<String>() }

  /**
   * 提交成功事件
   */
  val commitSuccessEvent by lazy { MutableLiveData<Unit>() }

  /**
   * 提交失败事件
   */
  val commitFailureEvent by lazy { MutableLiveData<Unit>() }

  /**
   * 用户输入的反馈内容空事件
   */
  val feedbackContentEmptyEvent by lazy { MutableLiveData<Unit>() }

  /**
   * 请求提交反馈信息
   */
  fun requestCommitFeedback() {
    // 用户未输入反馈内容
    if (feedbackContent.value.isNullOrEmpty()) {
      feedbackContentEmptyEvent.value = Unit
      return
    }
    // 拼接获取到的用户信息
    val description = "uid:${Hawk.get(HawkConfig.UserId, 0L)},appversion:${BuildConfig.VERSION_NAME},platform:${AppConfig.feedbackPlatform}"
    CommonDataSource.commitFeedback(
      this, CommitFeedbackBean.CommitFeedbackRequest(
        CommitFeedbackBean.Fields(
          CommitFeedbackBean.Assignee(AppConfig.feedbackAssignee),
          description,
          CommitFeedbackBean.Issuetype(AppConfig.feedbackIssueType),
          CommitFeedbackBean.Priority(AppConfig.feedbackPriority),
          CommitFeedbackBean.Project(AppConfig.feedbackProject),
          feedbackContent.value as String
        )
      )
    )
  }

  override fun commitFeedbackSuccess(result: CommitFeedbackBean.CommitFeedbackResponse?) {
    // 提交成功
    commitSuccessEvent.value = Unit
  }

  override fun commitFeedbackFailure(message: String?) {
    // 提交失败
    commitFailureEvent.value = Unit
  }
}
