package bums.lunatic.launcher.model

import android.location.Address
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import java.text.SimpleDateFormat
import java.util.Date

class LocationLog : RealmObject {
    var mFeatureName: String? = null
    var mAddressLines: RealmList<String> = realmListOf()
    var mAdminArea: String? = null
    var mSubAdminArea: String? = null
    var mLocality: String? = null
    var mSubLocality: String? = null
    var mThoroughfare: String? = null
    var mSubThoroughfare: String? = null
    var mPremises: String? = null
    var mPostalCode: String? = null
    var mCountryCode: String? = null
    var mCountryName: String? = null
    var mLatitude = 0.0
    var mLongitude = 0.0

    var mPhone: String? = null
    var mUrl: String? = null
    var time : Long = 0L
    var timeString : String? = null
    var userId : String? = null

    fun fillData(address: Address) {
        time = System.currentTimeMillis()
        timeString = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())
        mFeatureName = address.featureName
        mAddressLines.apply {
            for (i in 0..address.maxAddressLineIndex) {
                this.add(address.getAddressLine(i))
            }
        }
        mAdminArea = address.adminArea
        mSubAdminArea = address.subAdminArea
        mLocality = address.locality
        mSubLocality = address.subLocality
        mThoroughfare = address.thoroughfare
        mSubThoroughfare = address.subThoroughfare
        mPremises = address.premises
        mPostalCode = address.postalCode
        mCountryCode = address.countryCode
        mCountryName = address.countryName
        mLatitude = address.latitude
        mLongitude = address.longitude
        mPhone = address.phone
        mUrl = address.url

    }

    override fun toString(): String {
        val buffer = StringBuffer()
        buffer.append(mFeatureName).append("|").append("\n")
        buffer.append(mAddressLines.joinToString(" , ")).append("|").append("\n")
        buffer.append(mAdminArea).append("|").append("\n")
        buffer.append(mSubAdminArea).append("|").append("\n")
        buffer.append(mLocality).append("|").append("\n")
        buffer.append(mSubLocality).append("|").append("\n")
        buffer.append(mThoroughfare).append("|").append("\n")
        buffer.append(mSubThoroughfare).append("|").append("\n")
        buffer.append(mPremises).append("|").append("\n")
        buffer.append(mPostalCode).append("|").append("\n")
        buffer.append(mCountryCode).append("|").append("\n")
        buffer.append(mCountryName).append("|").append("\n")
        buffer.append(mLatitude).append("|").append("\n")
        buffer.append(mLongitude).append("|").append("\n")
        buffer.append(mPhone).append("|").append("\n")
        buffer.append(mUrl).append("|").append("\n")
        return buffer.toString()
    }
}

