//
//  FaqView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/11.
//

import SwiftUI
import RichText

struct FaqView: View {
    
    let faqs: [Faq]
    
    @State private var openIndex = -1
    
    var body: some View {
        VStack(spacing: 0) {
            ForEach(0..<faqs.count, id: \.self) { index in
                let faq = faqs[index]
                
                VStack(spacing: 0) {
                    HStack(alignment: .top, spacing: 6.7) {
                        Text("Q")
                            .font(.custom(Font.bold.rawValue, size: 13.3))
                            .foregroundColor(Color.button)
                        
                        Text(faq.question)
                            .font(.custom(Font.medium.rawValue, size: 14.7))
                            .foregroundColor(Color.grayee)
                            .fixedSize(horizontal: false, vertical: true)
                        
                        Spacer()
                        
                        Image(openIndex == index ? "btn_dropdown_up" : "btn_dropdown_down")
                            .resizable()
                            .frame(width: 20, height: 20)
                    }
                    .padding(.vertical, 20)
                    .padding(.horizontal, 6.7)
                    
                    if openIndex == index {
                        HStack(alignment: .top, spacing: 6.7) {
                            Text("A")
                                .font(.custom(Font.bold.rawValue, size: 13.3))
                                .foregroundColor(Color.button)
                                .padding(.top, 13.3)
                            
                            RichText(html: faq.answer)
                                .font(.custom(Font.medium.rawValue, size: 13.3))
                                .foregroundColor(Color.graybb)
                        }
                        .padding(.vertical, 20)
                        .padding(.horizontal, 6.7)
                        .background(Color.gray22)
                    }
                }
                .padding(.horizontal, 13.3)
                .contentShape(Rectangle())
                .onTapGesture {
                    if openIndex != index {
                        openIndex = index
                    } else {
                        openIndex = -1
                    }
                }
            }
        }
        .padding(.bottom, 40)
    }
}

struct FaqView_Previews: PreviewProvider {
    static var previews: some View {
        FaqView(
            faqs: [
                Faq(question: "질문1", answer: "답변1"),
                Faq(question: "질문2", answer: "답변2"),
                Faq(question: "질문3", answer: "답변3"),
                Faq(question: "질문4", answer: "답변4"),
                Faq(question: "질문5", answer: "답변5")
            ]
        )
    }
}
