package kr.lunaticbum.back.lun.controllers

import com.google.gson.Gson
import jakarta.servlet.http.HttpServletResponse
import kr.lunaticbum.back.lun.model.PostManageg
import kr.lunaticbum.back.lun.model.ResultMV
import kr.lunaticbum.back.lun.utils.LogService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URLDecoder

@RestController
@RequestMapping()
class Home {

    @Autowired
    lateinit var logService: LogService

    @Autowired
    private lateinit var postManageg: PostManageg

    @GetMapping("/","/home")
    fun home() : ResultMV {
        val vm = ResultMV("content/home")
        vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
            this.forEach {
                it.title = URLDecoder.decode(it.title)
                it.content = URLDecoder.decode(it.content)
                logService.log(Gson().toJson(it))
            }
        })
        vm.modelMap.put("path","/blog/viewer/")
        return vm
    }


    @GetMapping("/h2")
    fun home2() : ResultMV {
        val vm = ResultMV("content/index_ex")
        vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
            this.forEach {
                it.title = URLDecoder.decode(it.title)
                it.content = URLDecoder.decode(it.content)
                logService.log(Gson().toJson(it))
            }
        })
        return vm
    }

    @GetMapping("/left-sidebar")
    fun lside() : ResultMV {
        val vm = ResultMV("content/left-sidebar")
        vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
            this.forEach {
                it.title = URLDecoder.decode(it.title)
                it.content = URLDecoder.decode(it.content)
                logService.log(Gson().toJson(it))
            }
        })
        return vm
    }
    @GetMapping("/no-sidebar")
    fun nside() : ResultMV {
        val vm = ResultMV("content/no-sidebar")
        vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
            this.forEach {
                it.title = URLDecoder.decode(it.title)
                it.content = URLDecoder.decode(it.content)
                logService.log(Gson().toJson(it))
            }
        })
        return vm
    }

    @GetMapping("/right-sidebar")
    fun rside() : ResultMV {
        val vm = ResultMV("content/right-sidebar")
        vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
            this.forEach {
                it.title = URLDecoder.decode(it.title)
                it.content = URLDecoder.decode(it.content)
                logService.log(Gson().toJson(it))
            }
        })
        return vm
    }

    @GetMapping("/two-sidebar")
    fun bside() : ResultMV {
        val vm = ResultMV("content/two-sidebar")
        vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
            this.forEach {
                it.title = URLDecoder.decode(it.title)
                it.content = URLDecoder.decode(it.content)
                logService.log(Gson().toJson(it))
            }
        })
        return vm
    }




    @GetMapping("/login")
    fun login(response: HttpServletResponse) {
        response.sendRedirect("/user/login")
    }

    @GetMapping("/licenses")
    fun licenses() : ResultMV {
        val vm = ResultMV("content/licenses")
        return vm
    }

}