//
//  LiveRoomInfoGuestView.swift
//  SodaLive
//
//  Created by klaus on 2024/01/17.
//

import SwiftUI

struct LiveRoomInfoGuestView: View {
    
    let title: String
    let totalDonationCan: Int
    
    let isOnBg: Bool
    let isOnNotice: Bool
    
    let creatorId: Int
    let creatorNickname: String
    let creatorProfileUrl: String
    let speakerList: [LiveRoomMember]
    let muteSpeakerList: [UInt]
    let activeSpeakerList: [UInt]
    
    let isFollowing: Bool
    let isAdult: Bool
    
    let onClickQuit: () -> Void
    let onClickToggleBg: () -> Void
    let onClickShare: () -> Void
    let onClickFollow: (Bool) -> Void
    let onClickProfile: (Int) -> Void
    let onClickNotice: () -> Void
    let onClickTotalDonation: () -> Void
    
    var body: some View {
        ZStack {
            VStack(spacing: 13.3) {
                HStack(spacing: 5.3) {
                    LiveRoomOverlayStrokeTextButton(
                        text: "나가기",
                        textColor: Color.red,
                        strokeColor: Color.red,
                        strokeWidth: 1,
                        strokeCornerRadius: 5.3
                    ) { onClickQuit() }
                    
                    Spacer()
                    
                    LiveRoomOverlayStrokeTextToggleButton(
                        isOn: isOnBg,
                        onText: "배경 ON",
                        onTextColor: Color.button,
                        onStrokeColor: Color.button,
                        offText: "배경 OFF",
                        offTextColor: Color.graybb,
                        offStrokeColor: Color.graybb,
                        strokeWidth: 1,
                        strokeCornerRadius: 5.3
                    ) { onClickToggleBg() }
                    
                    LiveRoomOverlayStrokeImageButton(
                        imageName: "ic_share",
                        strokeColor: Color.graybb,
                        strokeWidth: 1,
                        strokeCornerRadius: 5.3
                    ) { onClickShare() }
                }
                
                HStack(spacing: 8) {
                    LiveRoomInfoCreatorView(
                        roomTitle: title,
                        creatorNickname: creatorNickname,
                        creatorProfileUrl: creatorProfileUrl,
                        isMute: muteSpeakerList.contains(UInt(creatorId)),
                        isAdult: isAdult,
                        isFollowing: isFollowing,
                        isActiveSpeaker: activeSpeakerList.contains(UInt(creatorId)),
                        isShowFollowingButton: true,
                        onClickFollow: { onClickFollow(isFollowing) },
                        onClickProfile: { onClickProfile(creatorId) }
                    )
                    .frame(width: 180, alignment: .leading)
                    
                    Spacer()
                    
                    ForEach(0..<speakerList.count, id: \.self) { index in
                        let speaker = speakerList[index]
                        
                        if speaker.id != UInt(creatorId) {
                            LiveRoomInfoSpeakerView(
                                nickname: speaker.nickname,
                                profileUrl: speaker.profileImage,
                                isMute: muteSpeakerList.contains(UInt(speaker.id)),
                                isActiveSpeaker: activeSpeakerList.contains(UInt(speaker.id)),
                                onClickProfile: { onClickProfile(speaker.id) }
                            )
                        }
                    }
                }
                
                HStack(spacing: 5.3) {
                    LiveRoomOverlayStrokeTextToggleButton(
                        isOn: isOnNotice,
                        onText: "공지",
                        onTextColor: .button,
                        onStrokeColor: .button,
                        offText: nil,
                        offTextColor: .graybb,
                        offStrokeColor: .graybb,
                        strokeWidth: 1,
                        strokeCornerRadius: 5.3,
                        onClick: { onClickNotice() }
                    )
                    
                    Spacer()
                    
                    HStack(spacing: 2.7) {
                        Image("ic_can")
                            .resizable()
                            .frame(width: 12, height: 12)
                        
                        Text("\(totalDonationCan)")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(.graybb)
                    }
                    .padding(.horizontal, 11)
                    .padding(.vertical, 5.3)
                    .overlay(
                        RoundedRectangle(cornerRadius: 5.3)
                            .stroke(Color.graybb, lineWidth: 1)
                    )
                    .onTapGesture { onClickTotalDonation() }
                }
            }
            
            if muteSpeakerList.contains(UInt(creatorId)) {
                Image("img_noti_mute")
            }
        }
        .padding(.horizontal, 13.3)
        .padding(.top, 16)
        .background(Color.gray22)
    }
}

struct LiveRoomInfoGuestView_Previews: PreviewProvider {
    static var previews: some View {
        LiveRoomInfoGuestView(
            title: "오늘의 라이브방송은 OOO입니다.",
            totalDonationCan: 123456,
            isOnBg: true,
            isOnNotice: false,
            creatorId: 1,
            creatorNickname: "도화",
            creatorProfileUrl: "https://cf.sodalive.net/profile/26/26-profile-ddf78b4d-0300-4c50-9c84-5d8a95fd5fe2-4892-1705256364320",
            speakerList: [
                LiveRoomMember(
                    id: 1,
                    nickname: "도화",
                    profileImage: "https://cf.sodalive.net/profile/26/26-profile-ddf78b4d-0300-4c50-9c84-5d8a95fd5fe2-4892-1705256364320",
                    role: .SPEAKER
                ),
                LiveRoomMember(
                    id: 2,
                    nickname: "청령",
                    profileImage: "https://cf.sodalive.net/profile/13/13-profile-fabb75e0-2870-4d99-900e-1d9aa63e605b-685-1704859996417",
                    role: .SPEAKER
                ),
                LiveRoomMember(
                    id: 3,
                    nickname: "LUNA",
                    profileImage: "https://cf.sodalive.net/profile/4679/4679-profile-41e83399-234e-4541-8591-f961a025cfaa-5819-1699536915310",
                    role: .SPEAKER
                ),
            ],
            muteSpeakerList: [],
            activeSpeakerList: [],
            isFollowing: false,
            isAdult: false,
            onClickQuit: {},
            onClickToggleBg: {},
            onClickShare: {},
            onClickFollow: { _ in },
            onClickProfile: { _ in },
            onClickNotice: {},
            onClickTotalDonation: {}
        )
    }
}