//
//  SeriesDetailIntroductionView.swift
//  SodaLive
//
//  Created by klaus on 4/29/24.
//

import SwiftUI
import SwiftUIFlowLayout

struct SeriesDetailIntroductionView: View {
    
    let width: CGFloat
    let seriesDetail: GetSeriesDetailResponse
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("키워드")
                .font(.custom(Font.bold.rawValue, size: 14.7))
                .foregroundColor(Color.grayee)
                .padding(.top, 16)
                .padding(.horizontal, 13.3)
            
            FlowLayout(mode: .scrollable, items: seriesDetail.keywordList, itemSpacing: 5.3) {
                SeriesKeywordChipView(keyword: $0)
            }
            .padding(.horizontal, 13.3)
            
            Rectangle()
                .frame(height: 6.7)
                .foregroundColor(Color.gray22)
            
            VStack(alignment: .leading, spacing: 13.3) {
                Text("작품소개")
                    .font(.custom(Font.bold.rawValue, size: 14.7))
                    .foregroundColor(Color.grayee)
                
                Text(seriesDetail.introduction)
                    .font(.custom(Font.medium.rawValue, size: 14.7))
                    .foregroundColor(Color.gray77)
                    .lineSpacing(4)
            }
            .padding(.horizontal, 13.3)
            
            Rectangle()
                .frame(height: 6.7)
                .foregroundColor(Color.gray22)
            
            VStack(alignment: .leading, spacing: 16) {
                Text("상세정보")
                    .font(.custom(Font.bold.rawValue, size: 14.7))
                    .foregroundColor(Color.grayee)
                
                HStack(spacing: 30) {
                    VStack(alignment: .leading, spacing: 13.3) {
                        Text("장르")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.gray77)
                        
                        Text("연령제한")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.gray77)
                        
                        if let _ = seriesDetail.writer {
                            Text("작가")
                                .font(.custom(Font.medium.rawValue, size: 14.7))
                                .foregroundColor(Color.gray77)
                        }
                        
                        if let _ = seriesDetail.studio {
                            Text("제작사")
                                .font(.custom(Font.medium.rawValue, size: 14.7))
                                .foregroundColor(Color.gray77)
                        }
                        
                        Text("연재")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.gray77)
                        
                        Text("출시일")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.gray77)
                    }
                    
                    VStack(alignment: .leading, spacing: 13.3) {
                        Text(seriesDetail.genre)
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.white)
                        
                        Text(seriesDetail.isAdult ? "19세 이상" : "전체연령가")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.white)
                        
                        if let writer = seriesDetail.writer {
                            Text(writer)
                                .font(.custom(Font.medium.rawValue, size: 14.7))
                                .foregroundColor(Color.white)
                        }
                        
                        if let studio = seriesDetail.studio {
                            Text(studio)
                                .font(.custom(Font.medium.rawValue, size: 14.7))
                                .foregroundColor(Color.white)
                        }
                        
                        Text(seriesDetail.publishedDaysOfWeek == "랜덤" ? seriesDetail.publishedDaysOfWeek : "\(seriesDetail.publishedDaysOfWeek)")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.white)
                        
                        Text(seriesDetail.publishedDate)
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.white)
                    }
                }
            }
            .padding(.horizontal, 13.3)
            
            VStack(alignment: .leading, spacing: 13.3) {
                Text("가격")
                    .font(.custom(Font.bold.rawValue, size: 14.7))
                    .foregroundColor(Color.grayee)
                
                HStack(spacing: 30) {
                    VStack(alignment: .leading, spacing: 13.3) {
                        Text("대여")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.gray77)
                        
                        Text("소장")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.gray77)
                    }
                    
                    VStack(alignment: .leading, spacing: 13.3) {
                        Text("\(calculatePriceInfo(seriesDetail.rentalMinPrice, seriesDetail.rentalMaxPrice)) (15일)")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.button)
                        
                        Text("\(calculatePriceInfo(seriesDetail.minPrice, seriesDetail.maxPrice))")
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.button)
                    }
                }
            }
            .padding(.horizontal, 13.3)
        }
    }
    
    func calculatePriceInfo(_ minPrice: Int, _ maxPrice: Int) -> String {
        if minPrice == maxPrice {
            if maxPrice == 0 {
                return "무료"
            } else {
                return "\(maxPrice)"
            }
        } else {
            return "\(minPrice == 0 ? "무료" : "\(minPrice)") ~ \(maxPrice)캔"
        }
    }
}
