//
//  ChatTitleView.swift
//  Sailassist
//
//  Created by Mamoru Sugita on 2023/11/09.
//

import SwiftUI

struct ChatTitleView: View {
    @Binding var isShowMember: Bool
    @Binding var isNotification: Bool
    @ObservedObject var message = SharingData.message

    var body: some View {
        VStack {
            HStack {
                Button{
                    
                }label: {
//                    Image("ink_02")
                }
                .disabled(true)
                .frame(width: 48, height: 48)
                
                Spacer()
                HStack(spacing: 8) {
                    VStack{
                        Text(SharingData.my.shipName)
                            .font(FontStyle.TitleL.font)

                        let usersName = userName()
                        HStack {
                            Text(usersName)
                                .font(FontStyle.RadioButtonText.font)

                            Button{
                                isShowMember.toggle()
                            }label: {
                                Image(systemName: isShowMember ? "chevron.up" : "chevron.down")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(width: 12, height: 7)
                                    .foregroundColor(ColorSet.Body.color)

                            }
                            .frame(width: 30, height: 30)
                        }
                    }
                    .foregroundColor(ColorSet.Body.color)
                    .frame(width: 162,height: 44)
                }
                
                Spacer()

                Button{
                    isNotification = chatNotification()
                }label: {
                    Image(isNotification ? "chat_alert" : "chat_alert_none")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 30, height: 30)
                }
                .frame(width: 48, height: 48)
            }
            .frame(height: 60)
            
            Divider()
                .background(ColorSet.LineColor04.color)
        }
    }

    /**
     * チャット名称をまとめる
     */
    func userName() -> String {
        var usersName = ""
        for user in message.users {
            usersName += user.name
            usersName += ","
        }
        return usersName
    }

    /**
     * チャット通知のON/OFF
     */
    func chatNotification() -> Bool {
        var isNotification = Preferences.ChatNotification
        isNotification.toggle()
        Preferences.ChatNotification = isNotification

        print(debug: "Test: \(isNotification)")
        return isNotification
    }
}

#Preview {
    ChatTitleView(isShowMember: .constant(false), isNotification: .constant(false))
}
