//
//  NotificationView.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/16.
//

import SwiftUI

struct NotificationView: View {
    @EnvironmentObject var selectedTabModel: SelectedTabModel
    @ObservedObject var pushHist = SharingData.pushHistory
    var body: some View {
        VStack(spacing: 0){
            TitleView()
            
            ScrollView {
                ForEach(pushHist.pushHistoryData.map{ $1 }.filter { $0.id != nil }.sorted{ ($0.id ?? 0) > ($1.id ?? 0) }, id: \.id){ data in
                    NotificationContentView(pushData: data)
                        .onTapGesture {
                            if let position = data.position {
                                if position.lat != nil && position.lon != nil {
                                    selectedTabModel.activeTab = .map
                                    pushHist.focusPushHistory = data.id
                                }
                            }
                        }

                    Divider()
                        .frame(height: 1)
                        .background(ColorSet.LineColor04.color)
                }
            }
            
            Spacer()
        }
        .background(ColorSet.BackgroundPrimary.color)
        .onAppear {
            let pushCount = pushHist.viewCnt
            print(debug: "\(pushCount)")
//            DispatchQueue.main.async {
//                pushHist.viewCnt = 0
//            }
        }
    }
    
    struct TitleView: View {
        var body: some View {
            VStack {
                HStack {
                    Button{
                        print(debug: "called")
                    }label: {
                    }
                    .disabled(true)
                    .frame(width: 48, height: 48)
                    
                    Spacer()
                    HStack(spacing: 0) {
                        Text("Notice")
                            .font(FontStyle.TitleL.font)
                            .foregroundColor(ColorSet.Body.color)
                    }
                    
                    Spacer()

                    Button{
                        print(debug: "called")
                    }label: {
                    }
                    .disabled(true)
                    .frame(width: 48, height: 48)
                }
                .frame(height: 60)
                
                Divider()
                    .background(ColorSet.LineColor04.color)
            }
        }
    }
}

#Preview {
    NotificationView()
        .environmentObject(SelectedTabModel())
}
