package cn.sagacloud.server.algorithm.test


import cn.sagacloud.server.algorithm.models.config.MyProperties
import cn.sagacloud.server.algorithm.models.jsonAnalyzer.AllComponents
import cn.sagacloud.server.algorithm.models.jsonAnalyzer.BaseObj
import cn.sagacloud.server.algorithm.models.modelFile.FloorModelView
import cn.sagacloud.server.algorithm.services.CommonService
import cn.sagacloud.server.algorithm.services.comparator.FloorComparator
import cn.sagacloud.server.algorithm.services.endpoint.FloorService
import cn.sagacloud.server.algorithm.services.http.HttpService
import cn.sagacloud.server.algorithm.services.json.JSONAnalyzerService
import com.sybotan.base.extensions.toJson
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner


@RunWith(SpringRunner::class)
@SpringBootTest
class ConfigTest {

    @Autowired
    private val prop: MyProperties? = null

    @Autowired
    private lateinit var floorService : FloorService

    @Autowired
    lateinit var floorComparator : FloorComparator

    @Autowired
    lateinit var jsonAnalyzerService : JSONAnalyzerService

    @Autowired
    lateinit var myProperties : MyProperties

    @Test
    fun test1() {

        println(prop!!.fileService.uploadPath)
    }

    @Test
    fun testMissingFloor(){
        val floorModelView1 = FloorModelView()
        floorModelView1.floorName = "B3"
        val floorModelView2 = FloorModelView()
        floorModelView2.floorName = "F3"
        val floorModelView3 = FloorModelView()
        floorModelView3.floorName = "B1M2"
        val floorModelView4 = FloorModelView()
        floorModelView4.floorName = "B1M3"
        val floorModelView5 = FloorModelView()
        floorModelView5.floorName = "B2M3"
        var list = arrayListOf<FloorModelView>(floorModelView1, floorModelView2, floorModelView3, floorModelView4, floorModelView5)
        val sortedWith = list.sortedWith(floorComparator)
        val addMissingFloor = floorService.addMissingFloor(sortedWith.toMutableList())
        addMissingFloor.forEach{
            println(it.floorName)
        }
    }

    @Test
    fun test11(){
            "a67ac1ec8fef11ea9dfea9a679dda25a"
        val id = "03e4dbc2192011ebb7c2517abf7cc6d61"
        var jsonData : BaseObj<AllComponents>?
        var size = 0L
        try {
            val url = CommonService.getExportJSONDownloadUrl(id, myProperties)
            println(url)
//            size = CommonService.getRemoteHttpFileSize(url)
            val fileStream = HttpService.getInputStream(url)
             //解析json
            jsonData = jsonAnalyzerService.analyzeJsonzFile(fileStream)

//            jsonData = JSONObject.parseObject(josn,BaseObj<AllComponents>().javaClass)

            val importResult = jsonAnalyzerService.importModelData(jsonData!!, "")
            println(importResult.toJson())
        }catch (e : Exception){
            println(e.message)
        }

    }
}
