//
//  WeekCalendarView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/14.
//

import SwiftUI

struct WeekCalendarView: View {
    
    @State private var selectedIndex = 0
    let action: (String) -> Void
    
    @ViewBuilder
    func DateItemView(index: Int, dateWithWeekDaySymbol: DateWithWeekDaySymbol) -> some View {
        VStack(spacing: 6.7) {
            Text(dateWithWeekDaySymbol.weekDaySymbol)
                .font(.custom(Font.medium.rawValue, size: 11.3))
                .foregroundColor(
                    self.selectedIndex == index ?
                        .white :
                        Color(hex: "e2e2e2")
                )
            
            Text(dateWithWeekDaySymbol.dayOfMonth)
                .font(.custom(Font.bold.rawValue, size: 15.3))
                .foregroundColor(
                    self.selectedIndex == index ?
                        .white :
                        Color(hex: "e2e2e2")
                )
        }
        .frame(width: 53.3)
        .frame(minHeight: 66.7)
        .background(index == selectedIndex ? Color(hex: "3bb9f1") : Color.clear)
        .cornerRadius(6.7)
        .onTapGesture {
            if self.selectedIndex != index {
                self.selectedIndex = index
                action(dateWithWeekDaySymbol.date)
            }
        }
    }
    
    var body: some View {
        VStack(spacing: 16.7) {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 6.7) {
                    ForEach(0..<7, id: \.self) { index in
                        let dateWithWeekDaySymbol = getDateFromCurrent(afterDay: index)
                        DateItemView(
                            index: index,
                            dateWithWeekDaySymbol: dateWithWeekDaySymbol
                        )
                    }
                }
            }
            .scaledToFit()
            
            Rectangle()
                .frame(height: 1)
                .foregroundColor(Color(hex: "909090").opacity(0.5))
        }
        .onAppear {
            self.selectedIndex = 0
            action(getDateFromCurrent(afterDay: 0).date)
        }
    }
    
    private func getDateFromCurrent(afterDay: Int) -> DateWithWeekDaySymbol {
        var calendar = Calendar.current
        calendar.locale = Locale(identifier: String(Locale.preferredLanguages[0].prefix(2)))
        
        let currentDate = Date()
        let futureDate = calendar.date(byAdding: .day, value: afterDay, to: currentDate)!
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        
        let dayOfMonthFormatter = DateFormatter()
        dayOfMonthFormatter.dateFormat = "d"
        
        let date = dateFormatter.string(from: futureDate)
        let dayOfMonth = dayOfMonthFormatter.string(from: futureDate)
        let day = calendar.component(.weekday, from: futureDate) - 1
        let weekDaySymbol = calendar.shortWeekdaySymbols[day]
        
        return DateWithWeekDaySymbol(
            date: date,
            dayOfMonth: dayOfMonth,
            weekDaySymbol: weekDaySymbol
        )
    }
}
