//
//  ContentDetailInfoView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/13.
//

import SwiftUI

struct ContentDetailInfoView: View {
    
    @Binding var isExpandDescription: Bool
    @Binding var isShowPreviewAlert: Bool
    
    let audioContent: GetAudioContentDetailResponse
    let onClickLike: () -> Void
    let onClickShare: () -> Void
    let onClickDonation: () -> Void
    
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            VStack(alignment: .leading, spacing: 8) {
                HStack(spacing: 5.3) {
                    Text(audioContent.themeStr)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .foregroundColor(Color(hex: "3bac6a"))
                        .padding(.horizontal, 5.3)
                        .padding(.vertical, 3.3)
                        .background(Color(hex: "28312b"))
                        .cornerRadius(2.6)
                    
                    if audioContent.isAdult {
                        Text("19")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color(hex: "e33621"))
                            .padding(.horizontal, 5.3)
                            .padding(.vertical, 3.3)
                            .background(Color(hex: "601d14"))
                            .cornerRadius(2.6)
                    }
                    
                    Spacer()
                    
                    if let orderType = audioContent.orderType, audioContent.existOrdered {
                        if let remainingTime = audioContent.remainingTime, orderType == .RENTAL {
                            HStack(spacing: 2.7) {
                                Image("ic_time_l")
                                
                                Text(remainingTime)
                                    .font(.custom(Font.medium.rawValue, size: 12))
                                    .foregroundColor(Color(hex: "909090"))
                            }
                        }
                        
                        Text(orderType == .KEEP ? "소장중" : "대여중")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(
                                orderType == .KEEP ?
                                Color(hex: "b1ef2c") :
                                    Color(hex: "9970ff")
                            )
                            .padding(.horizontal, 5.3)
                            .padding(.vertical, 3.3)
                            .background(
                                orderType == .KEEP ?
                                Color(hex: "26310f") :
                                    Color(hex: "30176f")
                            )
                            .cornerRadius(2.6)
                    }
                }
                
                Text(audioContent.title)
                    .font(.custom(Font.bold.rawValue, size: 16.7))
                    .foregroundColor(Color(hex: "d2d2d2"))
                    .lineSpacing(5)
                    .multilineTextAlignment(.leading)
                    .fixedSize(horizontal: false, vertical: true)
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            .padding(.top, 13.3)
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 8) {
                    HStack(spacing: 4) {
                        Image(
                            audioContent.isLike ?
                            "ic_audio_content_heart_pressed" :
                                "ic_audio_content_heart_normal"
                        )
                        
                        Text("\(audioContent.likeCount)")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "d2d2d2"))
                    }
                    .padding(.horizontal, 13.3)
                    .padding(.vertical, 5.3)
                    .background(Color(hex: "ffffff").opacity(0.1))
                    .cornerRadius(26.7)
                    .onTapGesture { onClickLike() }
                    
                    HStack(spacing: 4) {
                        Image("ic_audio_content_share")
                        
                        Text("공유")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color(hex: "d2d2d2"))
                    }
                    .padding(.horizontal, 13.3)
                    .padding(.vertical, 5.3)
                    .background(Color(hex: "ffffff").opacity(0.1))
                    .cornerRadius(26.7)
                    .onTapGesture { onClickShare() }
                    
                    if audioContent.isCommentAvailable {
                        HStack(spacing: 4) {
                            Image("ic_donation_white")
                                .resizable()
                                .frame(width: 13.3, height: 13.3)
                            
                            Text("후원")
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color(hex: "d2d2d2"))
                        }
                        .padding(.horizontal, 13.3)
                        .padding(.vertical, 5.3)
                        .background(Color(hex: "ffffff").opacity(0.1))
                        .cornerRadius(26.7)
                        .onTapGesture { onClickDonation() }
                    }
                }
            }
            .padding(.top, 13.3)
            
            ZStack {
                VStack(spacing: 8) {
                    if audioContent.tag.count > 0 {
                        Text(audioContent.tag)
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color(hex: "9970ff"))
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    
                    Text(audioContent.detail)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .foregroundColor(Color(hex: "777777"))
                        .lineLimit(isExpandDescription ? nil : 3)
                        .lineSpacing(5)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .onTapGesture { isExpandDescription.toggle() }
                }
                .padding(.top, 13.3)
                
                if isShowingPreviewAlert() {
                    HStack(spacing: 0) {
                        Text("미리듣기 중입니다.\n콘텐츠 구매 후 전체를 감상해 보세요.")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color(hex: "bbbbbb"))
                            .lineSpacing(5)
                        
                        Spacer()
                        
                        Image("ic_circle_x_white")
                            .onTapGesture { isShowPreviewAlert = false }
                    }
                    .padding(13.3)
                    .background(Color(hex: "1e0e45").opacity(0.89))
                    .cornerRadius(5.3)
                    .overlay(
                        RoundedRectangle(cornerRadius: 5.3)
                            .stroke(lineWidth: 1)
                            .foregroundColor(Color(hex: "9970ff"))
                    )
                }
            }
        }
    }
    
    private func isShowingPreviewAlert() -> Bool {
        return isShowPreviewAlert &&
        audioContent.creator.creatorId != UserDefaults.int(forKey: .userId) &&
        !audioContent.existOrdered &&
        audioContent.price > 0
    }
}