//
//  MenuInformationView.swift
//  Sailassist
//
//  Created by Mamoru Sugita on 2023/12/26.
//

import SwiftUI

struct MenuInformationView: View {
    @Binding var path : [MenuPath]
    @ObservedObject var info = SharingData.information

    var body: some View {
        VStack{
            MenuTitleView(path: $path, title: MenuPath.Information.title)

            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([]))
}
