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

import SwiftUI
import Kingfisher

struct LiveRoomInfoCreatorView: View {
    
    let roomTitle: String
    let creatorNickname: String
    let creatorProfileUrl: String
    
    let isMute: Bool
    let isAdult: Bool
    let isFollowing: Bool
    let isActiveSpeaker: Bool
    let isShowFollowingButton: Bool
    
    let onClickFollow: () -> Void
    let onClickProfile: () -> Void
    
    var body: some View {
        HStack(spacing: 5.3) {
            ZStack(alignment: .center) {
                KFImage(URL(string: creatorProfileUrl))
                    .cancelOnDisappear(true)
                    .downsampling(
                        size: CGSize(
                            width: 33.3,
                            height: 33.3
                        )
                    )
                    .resizable()
                    .frame(width: 33.3, height: 33.3)
                    .clipShape(Circle())
                    .overlay(
                        Circle()
                            .stroke(
                                Color.button,
                                lineWidth: isActiveSpeaker ? 3 : 0
                            )
                    )
                    .onTapGesture { onClickProfile() }
                
                if isMute {
                    Image("ic_mute")
                        .resizable()
                        .frame(width: 33.3, height: 33.3)
                }
            }
            
            VStack(alignment: .leading, spacing: 6.7) {
                HStack(spacing: 2.7) {
                    if isAdult {
                        Text("19")
                            .font(.custom(Font.bold.rawValue, size: 8))
                            .foregroundColor(.white)
                            .padding(.vertical, 2.8)
                            .padding(.horizontal, 2)
                            .background(Circle().foregroundColor(Color.mainRed2))
                    }
                    
                    Text(roomTitle)
                        .font(.custom(Font.bold.rawValue, size: 12))
                        .foregroundColor(.grayee)
                        .lineLimit(1)
                }
                
                HStack(spacing: 5.3) {
                    Text(creatorNickname)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .foregroundColor(.gray77)
                        .lineLimit(1)
                    
                    if isShowFollowingButton {
                        Image(isFollowing ? "btn_following" : "btn_follow")
                            .onTapGesture { onClickFollow() }
                    }
                }
            }
        }
        .padding(8)
        .frame(width: screenSize().width / 2.5, alignment: .leading)
        .overlay(
            RoundedRectangle(cornerRadius: 5.3)
                .stroke(Color.graybb, lineWidth: 1)
        )
    }
}

struct LiveRoomInfoCreatorView_Previews: PreviewProvider {
    static var previews: some View {
        LiveRoomInfoCreatorView(
            roomTitle: "qwer",
            creatorNickname: "도화",
            creatorProfileUrl: "https://cf.sodalive.net/profile/26/26-profile-ddf78b4d-0300-4c50-9c84-5d8a95fd5fe2-4892-1705256364320",
            isMute: false,
            isAdult: false,
            isFollowing:  true,
            isActiveSpeaker: true,
            isShowFollowingButton: true,
            onClickFollow: {},
            onClickProfile: {}
        )
    }
}
