//
//  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
    @State var isChatWarning: Bool = false

    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()
                    if isNotification {
                        isChatWarning = false
                    } else {
                        isChatWarning = true
                    }
                }label: {
                    Image(isNotification ? "chat_alert" : "chat_alert_none")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 30, height: 30)
                }
                .frame(width: 48, height: 48)
            }
            .frame(height: 60)
            .alert(isPresented: $isChatWarning) {
                Alert(title: Text("Caution"), message: Text("You can’t receive notifications even in warning mode."), dismissButton: .default(Text("OK"), action:{}))
            }

            Divider()
                .background(ColorSet.LineColor04.color)
        }
    }

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

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

        let notificationTags = NotificationTags()
        notificationTags.addTags()

        return isNotification
    }
}

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