package com.yzx.escreen.presenter

import com.blankj.utilcode.util.LogUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.yzx.escreen.config.Config
import com.yzx.escreen.config.YzxInterface
import com.yzx.escreen.model.LeaveID
import com.yzx.escreen.model.Pager
import com.yzx.escreen.model.StuLeave
import com.yzx.escreen.model.User
import com.yzx.escreen.model.Vacation
import com.yzx.escreen.presenter.base.BasePresenter
import com.yzx.escreen.presenter.base.IView
import com.yzx.escreen.presenter.base.JsonCallBack
import com.yzx.escreen.presenter.base.SimpleResponse
import com.yzx.escreen.presenter.base.YzxResponse
import java.util.Calendar

class HomePresenter(view: HomeView) : BasePresenter<HomeView>(view) {


    fun getLeaveList(
        pageNo: Int,
        keyword: String,
        status: String,
        add_time: String,
        listType: Int,
        is_hide: Int
    ) {
        if (User.getUser().token.isEmpty()) {
            return
        }
        val calendar = Calendar.getInstance()
        var endTime: Long = 0
        var startTime: Long = 0
        when (add_time) {
            "today" -> {
                calendar.set(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE),
                    23,
                    59,
                    59
                )
                endTime = calendar.timeInMillis / 1000 // 当前时间戳
                calendar.set(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE),
                    0,
                    0,
                    0
                )
                startTime = calendar.timeInMillis / 1000 // 开始时间戳
            }

            "yesterday" -> {
                calendar.add(Calendar.DAY_OF_YEAR, -1) // 将日期往前推1天
                calendar.set(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE),
                    23,
                    59,
                    59
                )
                endTime = calendar.timeInMillis / 1000 // 当前时间戳
                calendar.set(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE),
                    0,
                    0,
                    0
                )
                startTime = calendar.timeInMillis / 1000 // 开始时间戳
            }

            "last7Days" -> {
                calendar.set(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE),
                    23,
                    59,
                    59
                )
                endTime = calendar.timeInMillis / 1000 // 当前时间戳
                calendar.add(Calendar.DAY_OF_YEAR, -6) // 将日期往前推6天
                calendar.set(
                    calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DATE),
                    0,
                    0,
                    0
                )
                startTime = calendar.timeInMillis / 1000 // 近七天的开始时间戳
            }
        }
        val params = HashMap<String, String>()
        params["keyword"] = keyword
        params["status"] = status
        if (listType == 1) {
            params["page_size"] = "6"
        } else {
            params["page_size"] = "20"
        }
        params["page_no"] = "$pageNo"
        if (add_time != "") {
            params["start_time"] = "$startTime"
            params["end_time"] = "$endTime"
        }

        if (is_hide == 0 || is_hide == 1) {
            params["is_hide"] = "${is_hide}"
        }

        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_STU_LEAVE_LIST}"
        OkGo.post<YzxResponse<StuLeave>>(url)
            .tag(this)
            .params(params)
            .execute(object : JsonCallBack<YzxResponse<StuLeave>>() {


                override fun onSuccess(response: Response<YzxResponse<StuLeave>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onLeaveListSuccess(response.body().list, response.body().pager)
                    }
                }

                override fun onError(response: Response<YzxResponse<StuLeave>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onLeaveListError(response?.body()?.msg ?: "")
                }
            })
    }

    fun getLeaveShowIds() {
        if (User.getUser().token.isEmpty()) {
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_GET_SHOW_BOX_LIST}"
        OkGo.post<YzxResponse<LeaveID>>(url)
            .tag(this)
            .execute(object : JsonCallBack<YzxResponse<LeaveID>>() {
                override fun onSuccess(response: Response<YzxResponse<LeaveID>>?) {
                    if (response?.isSuccessful == true) {
                        mView?.onLeaveIDsSuccess(response.body().list)
                        LogUtils.d("getLeaveShowIds", response.body().list)
                    }
                }

                override fun onError(response: Response<YzxResponse<LeaveID>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onLeaveListError(response?.body()?.msg ?: "")
                }
            })
    }

    fun getDetail(id: Int) {
        if (User.getUser().token.isEmpty()) {
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_STU_LEAVE_INFO_DETAIL}"
        OkGo.post<YzxResponse<StuLeave>>(url)
            .tag(this)
            .params("id", id)
            .execute(object : JsonCallBack<YzxResponse<StuLeave>>() {


                override fun onSuccess(response: Response<YzxResponse<StuLeave>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onLeaveDetailSuccess(response.body().data)
                    }
                }

                override fun onError(response: Response<YzxResponse<StuLeave>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onLeaveDetailError(response?.body()?.msg ?: "")
                }
            })
    }

    fun postVacation(data: Vacation) {
        if (User.getUser().token.isEmpty()) {
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_POST_LEAVE_VACATION}"
        OkGo.post<SimpleResponse>(url)
            .tag(this)
            .params("id", data.id)
            .params("vacation_type", data.vacation_type)
            .params("vacation_time", data.vacation_time)
            .params("vacation_remark", data.vacation_remark)
            .params("vacation_attachment", data.vacation_attachment)
            .execute(object : JsonCallBack<SimpleResponse>() {


                override fun onSuccess(response: Response<SimpleResponse>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.postVacationSuccess("销假成功！")
                    }
                }

                override fun onError(response: Response<SimpleResponse>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.postVacationError(response?.body()?.msg ?: "销假失败，请重试！")
                }
            })
    }

    fun  outSchool(item:StuLeave){
        if (User.getUser().token.isEmpty()) {
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_POST_HIDE_LEAVE}"
        OkGo.post<SimpleResponse>(url)
            .tag(this)
            .params("is_hide", 1)
            .params("id", item.id)
            .execute(object : JsonCallBack<SimpleResponse>() {


                override fun onSuccess(response: Response<SimpleResponse>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.postHideSuccess("已放行！")
                    }
                }

                override fun onError(response: Response<SimpleResponse>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.postHideError(response?.body()?.msg ?: "操作失败，请重试！")
                }
            })
    }

}

interface HomeView : IView {
    fun onLeaveListSuccess(list: MutableList<StuLeave>, pager: Pager)
    fun onLeaveListError(error: String)
    fun onLeaveDetailSuccess(detail: StuLeave)
    fun onLeaveDetailError(error: String)
    fun onLeaveIDsSuccess(list: MutableList<LeaveID>)
    fun postVacationSuccess(msg: String)
    fun postVacationError(msg: String)

    fun postHideSuccess(msg: String)
    fun postHideError(msg: String)
}