package com.pan_american.android.ui.efficiency_tools.itinerary

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.base.BaseActivity
import com.pan_american.android.databinding.ActivityItineraryBinding
import com.pan_american.android.databinding.LayoutTitleBinding

class ItineraryActivity : BaseActivity<ActivityItineraryBinding>(),
    CalendarView.OnCalendarSelectListener, CalendarView.OnYearChangeListener,
    CalendarView.OnMonthChangeListener {

    private lateinit var titleBinding: LayoutTitleBinding

    //日历组件
    private lateinit var itineraryCalendar: CalendarView

    //日历组件 年月显示
    private lateinit var yearMonthText: TextView

    //日历组件 年月日
    private var year: Int = 0
    private var month: Int = 0
    private var day: Int = 0

    //日程标题
    private lateinit var itineraryListTitle: TextView

    override fun getViewBinding() = ActivityItineraryBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        screenAdaptation(binding)

        initCalendar()
        initTitle()
        initViews()
    }

    override fun initTitle() {
        titleBinding = LayoutTitleBinding.bind(binding.root).apply {
            titleText.text = resources.getString(R.string.itinerary)

            if (OASystem.authorization(OASystem.ITINERARY, OASystem.ADD)) {
                addButton.apply {
                    visibility = View.VISIBLE
                    setOnClickListener {
                        val intent = Intent(OASystem.context, AddItineraryActivity::class.java)
                        startActivity(intent)
                    }
                }
            }

            backButton.setOnClickListener {
                back()
            }
        }
    }

    override fun initViews() {
        //日程日期显示
        itineraryListTitle = binding.itineraryListTitle
        itineraryListTitle.text = String.format(resources.getString(R.string.today), month, day)
    }

    private fun initCalendar() {
        itineraryCalendar = binding.itineraryCalendar
        yearMonthText = binding.yearMonthText

        year = itineraryCalendar.curYear
        month = itineraryCalendar.curMonth
        day = itineraryCalendar.curDay

        yearMonthText.text = String.format(resources.getString(R.string.year_month), year, month)

        itineraryCalendar.setOnYearChangeListener(this)
        itineraryCalendar.setOnMonthChangeListener(this)
        itineraryCalendar.setOnCalendarSelectListener(this)
    }

    override fun onYearChange(year: Int) {
        this.year = year
        yearMonthText.text = String.format(resources.getString(R.string.year_month), year, month)
    }

    override fun onMonthChange(year: Int, month: Int) {
        this.month = month
        yearMonthText.text = String.format(resources.getString(R.string.year_month), year, month)
    }

    override fun onCalendarOutOfRange(calendar: Calendar?) {

    }

    override fun onCalendarSelect(calendar: Calendar?, isClick: Boolean) {
        if (calendar != null) {
            if (calendar.month == itineraryCalendar.curMonth && calendar.day == itineraryCalendar.curDay) {
                itineraryListTitle.text =
                    String.format(resources.getString(R.string.today), calendar.month, calendar.day)
            } else {
                itineraryListTitle.text = String.format(resources.getString(R.string.month_day), calendar.month, calendar.day)
            }
        }
    }
}