package com.persagy.revit.services.message

import com.alibaba.fastjson.JSON
import com.persagy.revit.models.config.MyProperties
import com.persagy.revit.models.message.MessageBody
import com.persagy.revit.models.message.MessageEntity
import com.persagy.revit.models.message.MessageLink
import com.persagy.revit.models.message.MessageType
import com.persagy.revit.services.http.HttpService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
class MessageService {

    @Autowired
    lateinit var properties: MyProperties

    val moduleName = "Model"

    val httpService = HttpService

    fun sendMessage(messageEntity: MessageEntity){
        httpService.sendPost(properties.message.createPath, JSON.toJSONString(messageEntity))
    }

    /**
     * 构建检查信息, success时没有下载文件, failure时有下载文件
     */
    fun buildCheckErrorMessage(projectId: String?, userId: String, title: String?, message: String?, downloadUrl: String?, folderFloorName: Pair<String?, String?>) : MessageEntity {
        val type : MessageType = MessageType.Error
        return buildMessageEntity(title, type, projectId, arrayListOf(userId), message, "下载", downloadUrl, "${folderFloorName.first}${folderFloorName.second}模型文件问题.xlsx")
    }

    fun buildMessageEntity(title : String?, type : MessageType, projectId : String?, userList : ArrayList<String>?,
                           message : String?, linkName : String?, downloadUrl : String?, fileName : String?) : MessageEntity {
        val messageEntity = MessageEntity()
        messageEntity.title = title ?: ""
        messageEntity.module = moduleName
        messageEntity.type = type
        messageEntity.projectId = projectId ?: ""
        val messageBody = MessageBody()
        messageBody.messageStr = message ?: ""
        messageEntity.content = messageBody
        messageEntity.userList = userList
        val messageLink = MessageLink()
        messageLink.fileName = fileName ?: ""
        messageLink.name = linkName ?: ""
        messageLink.url = downloadUrl ?: ""
        messageBody.buttonList = ArrayList()
        messageBody.buttonList!!.add(messageLink)
        return messageEntity
    }


    fun buildMessageEntityWithoutLink(title : String?, type : MessageType, projectId : String?, userList : ArrayList<String>?,
                           message : String?) : MessageEntity {
        val messageEntity = MessageEntity()
        messageEntity.title = title ?: ""
        messageEntity.module = moduleName
        messageEntity.type = type
        messageEntity.projectId = projectId ?: ""
        val messageBody = MessageBody()
        messageBody.messageStr = message ?: ""
        messageEntity.content = messageBody
        messageEntity.userList = userList
        messageBody.buttonList = ArrayList()
        return messageEntity
    }

    /**
     * 给前台的刷新通知消息
     */
    fun buildRefreshMessage(projectId : String?, userList : String) : MessageEntity {
        val messageEntity = MessageEntity()
        messageEntity.title = ""
        messageEntity.module = moduleName
        messageEntity.type = MessageType.Refresh
        messageEntity.projectId = projectId ?: ""
        val messageBody = MessageBody()
        messageBody.messageStr = ""
        messageEntity.content = messageBody
        messageEntity.userList = arrayListOf(userList)
        messageBody.buttonList = ArrayList()
        return messageEntity
    }


    fun buildExportErrorMessage(projectId: String?, userId: String, title : String?, message: String): MessageEntity {
        var type : MessageType = MessageType.Error
        return buildMessageEntityWithoutLink(title, type, projectId, arrayListOf(userId), message)
    }

    fun buildMessage(title: String?, checkResult: Boolean, projectId: String?, userId: String, message: String?, downloadUrl: String?, folderFloorName: Pair<String?, String?>): MessageEntity {
        var type : MessageType
        if(checkResult) {
            type = MessageType.Success
            return buildMessageEntityWithoutLink(title, type, projectId, arrayListOf(userId), message)
        }
        else{
            type = MessageType.Error
            return buildMessageEntity(title, type, projectId, arrayListOf(userId), message, "下载", downloadUrl, "${folderFloorName.first}${folderFloorName.second}模型文件问题.xlsx")
        }
    }

    fun buildMultiModelCheckErrorMessage(projectId: String?, userId: String, title: String?, downloadUrl: String?, folderFloorName: Pair<String?, String?>): MessageEntity {
        var type : MessageType = MessageType.Error
        return buildMessageEntity(title, type, projectId, arrayListOf(userId), "轴网和标高检查未通过", "下载", downloadUrl, "${folderFloorName.first}${folderFloorName.second}模型文件问题.xlsx")
    }
}