package kr.lunaticbum.back.lun.model

import com.fasterxml.jackson.annotation.JsonProperty
import com.google.gson.annotations.SerializedName
import kr.lunaticbum.back.lun.utils.LogService
import lombok.AllArgsConstructor
import lombok.Data
import lombok.NoArgsConstructor
import org.bson.codecs.pojo.annotations.BsonIgnore
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.Query
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono


// import com.fasterxml.jackson.databind.ObjectMapper; // version 2.11.1
// import com.fasterxml.jackson.annotation.JsonProperty; // version 2.11.1
/* ObjectMapper om = new ObjectMapper();
Root root = om.readValue(myJsonString, Root.class); */
class Chat {
    var id: Long = 0
    var first_name: String? = null
    var last_name: String? = null
    var username: String? = null
    var type: String? = null
}

class Entity {
    var offset: Int = 0
    var length: Int = 0
    var type: String? = null
}

class From {
    var id: Long = 0
    var is_bot: Boolean = false
    var first_name: String? = null
    var last_name: String? = null
    var username: String? = null
    var language_code: String? = null
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "TelegramMessage")
class Message {
    @Id
    var message_id: String = ""

    @BsonIgnore
    var from: From? = null
    @BsonIgnore
    var chat: Chat? = null
    var date: Long = 0
    var text: String? = null
    @BsonIgnore
    var entities: ArrayList<Entity>? = null
    var location : TelegramLocation? = null
//    var location : Map<String,String>? = null
}

class TelegramLocation {
    @SerializedName("longitude")
    var longitude: Double? = null

    @SerializedName("latitude")
    var latitude: Double? = null

    @SerializedName("horizontal_accuracy")
    var horizontalAccuracy: Double? = null


    @SerializedName("live_period")
    var livePeriod: Int? = null


    @SerializedName("heading")
    var heading: Int? = null


    @SerializedName("proximity_alert_radius")
    var proximityAlertRadius: Int? = null
}

class Result {
    var update_id: Int = 0
    var message: Message? = null
}

class TelegramUpdate {
    fun isSucces() = ok == true

    var ok: Boolean = false
    var result: ArrayList<Result>? = null
}

@Repository
interface TelegramRepository : ReactiveMongoRepository<Message,String> {
    @Query("{id :?0}")
    override fun findById(id: String): Mono<Message>

    @Query("{id :?0}")
    fun count(id: Int): Mono<Long>

    fun save(message: Message): Mono<Message>
}
interface MsgService {
    fun findById(id: String): Mono<Message>?
}

@Service
class TelegramMsgService : MsgService {
    @Autowired
    private lateinit var logService: LogService

    @Autowired
    private lateinit var telegramRepository: TelegramRepository



    override fun findById(id: String): Mono<Message>? {
        return telegramRepository.findById(id)
    }


    fun has(id: Int): Mono<Long>? {
        return telegramRepository.count(id)
    }

    fun save(msg: Message) {
        println("saved msg before ${msg}")
        telegramRepository.save(msg).subscribe( {  println("saved msg after ${it}") },{e -> e.printStackTrace()},{
            println("saved msg  comp")
        })

    }
}

class GoogleSearchResult {

    var items : ArrayList<GSRItem>? = null
}

typealias GSRMeta = Map<String,String>

class GSRItem{
    var kind  : String? = null //": "customsearch#result",
    var title  : String? = null //": "전국 현재 날씨",
    var htmlTitle  : String? = null //": "전국 현재 <b>날씨</b>",
    var link  : String? = null //": "https://www.accuweather.com/ko/us/united-states-weather",
    var displayLink  : String? = null //": "www.accuweather.com",
    var snippet  : String? = null //": "개인정보 보호정책과 쿠키 사용 정책이 업데이트되었습니다. 이해합니다. ComScore.",
    var htmlSnippet  : String? = null //": "개인정보 보호정책과 쿠키 사용 정책이 업데이트되었습니다. 이해합니다. ComScore.",
    var formattedUrl  : String? = null //": "https://www.accuweather.com/ko/us/united-states-weather",
    var htmlFormattedUrl : String? = null //": "https://www.accuweather.com/ko/us/united-states-weather",
    var pagemap : GSRItemPageMap? = null
}
class GSRItemPageMap {
    var cse_thumbnail : ArrayList<Map<String,String>>? = null
    var metatags :  ArrayList<Map<String,String>>? = null
    var cse_image : ArrayList<Map<String,String>>? = null
}

