//
//  NoticeListView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import SwiftUI
import RichText

struct NoticeListView: View {
    @ObservedObject var viewModel = NoticeListViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "공지사항")
                
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: 0) {
                        ForEach(viewModel.notices, id: \.self) { notice in
                            VStack(alignment: .leading, spacing: 6.7) {
                                Spacer()
                                
                                Text(notice.title)
                                    .font(.custom(Font.medium.rawValue, size: 14.7))
                                    .foregroundColor(Color(hex: "eeeeee"))
                                    .padding(.horizontal, 13.3)
                                
                                Text(notice.date)
                                    .font(.custom(Font.medium.rawValue, size: 12))
                                    .foregroundColor(Color(hex: "525252"))
                                    .padding(.horizontal, 13.3)
                                
                                Spacer()
                                
                                Rectangle()
                                    .frame(height: 1)
                                    .foregroundColor(Color(hex: "909090").opacity(0.5))
                            }
                            .padding(.horizontal, 13.3)
                            .frame(width: screenSize().width, height: 80)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                AppState.shared.setAppStep(step: .noticeDetail(notice: notice))
                            }
                        }
                    }
                }
                .padding(.vertical, 13.3)
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .padding(.horizontal, 6.7)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color(hex: "3bb9f1"))
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .fixedSize(horizontal: false, vertical: true)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
        .onAppear {
            viewModel.getNotices()
        }
    }
}

struct NoticeListView_Previews: PreviewProvider {
    static var previews: some View {
        NoticeListView()
    }
}