package com.persagy.revit.services.comparator

import com.persagy.revit.models.modelFile.FloorModelView
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
class FloorComparator : Comparator<FloorModelView> {
    private val positive = 1
    private val negative = -1
    override fun compare(o1: FloorModelView?, o2: FloorModelView?): Int {
        try {
            if (o1 == null || o1.floorName == null)
                return negative
            if (o2 == null || o2.floorName == null)
                return positive
            val floor1 = o1.floorName!!
            val floor2 = o2.floorName!!
            when {
                floor1.startsWith("RFM", ignoreCase = true) -> {
                    when {
                        floor2.startsWith("RFM", ignoreCase = true) ->{
                            val middle1 = floor1.substring(3).toInt()
                            val middle2 = floor2.substring(3).toInt()
                            return middle1.compareTo(middle2)
                        }
                        else ->
                            return negative
                    }
                }
                floor1.startsWith("RF", ignoreCase = true) ->{
                    when{
                        floor2.startsWith("RFM", ignoreCase = true) ->{
                            return positive
                        }
                        floor2.startsWith("RF", ignoreCase = true) -> return 0
                        else -> return negative
                    }
                }
                floor1.startsWith("F", ignoreCase = true) -> {
                    when {
                        floor2.startsWith("RF", ignoreCase = true) -> return positive
                        floor2.startsWith("F", ignoreCase = true) -> {
                            val floorEntity1 = FloorEntity.getFloorEntity(floor1)
                            val floorEntity2 = FloorEntity.getFloorEntity(floor2)
                            if(floorEntity1 == null)
                                return positive
                            if(floorEntity2 == null)
                                return negative
                            return floorEntity2.compare(floorEntity1)
                        }
                        floor2.startsWith("B", ignoreCase = true) -> return negative
                    }
                }
                floor1.startsWith("B", ignoreCase = true) -> {
                    when{
                        floor2.startsWith("RF", ignoreCase = true) -> return positive
                        floor2.startsWith("F", ignoreCase = true) -> return positive
                        floor2.startsWith("B", ignoreCase = true) ->{
                            val floorEntity1 = FloorEntity.getFloorEntity(floor1)
                            val floorEntity2 = FloorEntity.getFloorEntity(floor2)
                            if(floorEntity1 == null)
                                return negative
                            if(floorEntity2 == null)
                                return positive
                            return floorEntity1.compare(floorEntity2)
                        }
                    }
                }
            }
        }catch (e : Exception){
        }
        return 0
    }
}

class FloorEntity{
    fun compare(floor2: FloorEntity): Int {
        when {
            floorNumber > floor2.floorNumber ->
                return 1
            floorNumber == floor2.floorNumber ->
                return floor2.middleNumber.compareTo(middleNumber)
            floorNumber < floor2.floorNumber ->
                return -1
        }
        return 0
    }

    var floorNumber : Int = 0
    var middleNumber : Int = 0

    companion object{
        fun getFloorEntity(str : String) : FloorEntity?{
            var floor = FloorEntity()
            var arr = str.split("F|M|f|m|B|b".toRegex())
            when {
                arr.size == 2 -> {
                    floor.floorNumber = arr[1].toInt()
                }
                arr.size == 3 -> {
                    floor.floorNumber = arr[1].toInt()
                    floor.middleNumber = arr[2].toInt()
                }
                else -> return null
            }
            return floor
        }
    }
}