//
//  InformationView.swift
//  Sailassist
//
//  Created by 三浦薫巳 on 2024/03/01.
//

import SwiftUI

struct InformationView: View {
    @ObservedObject var info = SharingData.information

    var body: some View {
        VStack{
            ScrollView{
                VStack(spacing: 0){
                    ForEach(info.content.sorted{ $0.informationId < $1.informationId }, id: \.informationId){ info in
                        InformationView(title: info.title, bodyStr: info.detail)
                    }
                }
                .padding()
                .background(.black)
            }
            .onAppear(perform: {
                let information = GetInformation()
                information.start()
            })
        }
    }

    private struct InformationView: View {
        var title: String
        var bodyStr: String
        @State var isExtend = false
        var body: some View {
            VStack(alignment: .leading, spacing: 0){
                HStack{
                    Text(title)
                        .foregroundColor(.white)

                    Spacer()

                    Button {
                        isExtend.toggle()
                    } label: {
                        Image(systemName: isExtend ? "minus" : "plus")
                            .foregroundColor(.white)
                    }
                }
                .padding()
                .background(.gray)

                if isExtend {
                    Text(bodyStr)
                        .multilineTextAlignment(.leading)
                        .padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
                        .foregroundColor(.black)
                }
            }
            .background(.white)
        }
    }
}

#Preview {
    MenuInformationView(path: .constant([]))
}
