import SwiftUI

struct CV: View {
    
    @Environment(\.colorScheme)
    var colorScheme: ColorScheme

    let info: CVInfo

    let style: CVStyle

    private var twoColumnSpacing: CGFloat {
        style.columnSpacing
    }

    var body: some View {
        VStack {
            TopView(
                info: info.top,
                style: style.header,
                accent: style.accentColor)
                .frame(height: style.header.height)
            Rectangle()
                .fill(style.accentColor)
                .frame(height: style.header.lineWidth)
            GeometryReader { geo in
                let columnWidth = max(0, (geo.size.width - twoColumnSpacing)) / 2
                HStack(alignment: .top, spacing: twoColumnSpacing) {
                    VStack(alignment: .leading) {
                        TitledCareerSection(
                            style: style.section,
                            accent: style.accentColor,
                            content: info.work)
                        TitledCareerSection(
                            style: style.section,
                            accent: style.accentColor,
                            content: info.education)
                    }.frame(width: columnWidth)
                    VStack(alignment: .leading) {
                        TitledSection(
                            title: info.publications.title,
                            spacing: style.section.titleSpacing) {
                                ForEach(info.publications.items) { item in
                                PublicationView(
                                    info: item,
                                    borderSpacing: style.section.borderSpacing,
                                    borderWidth: style.section.borderWidth,
                                    accent: style.accentColor)
                                    .padding(.bottom, style.section.bottomSpacing)
                            }
                        }
                        TitledIconSection(
                            content: info.skills,
                            titleSpacing: style.section.titleSpacing,
                            width: columnWidth,
                            style: style.skillStyle)
                        TitledTextSection(
                            content: info.about,
                            titleSpacing: style.section.titleSpacing,
                            paragraphSpacing: style.section.paragraphSpacing)
                    }.frame(width: columnWidth)
                }
            }
            Spacer(minLength: 0)
            Text(info.footer)
            .font(.footnote)
            .foregroundColor(colorScheme.secondaryColor)
        }
        .padding()
        .aspectRatio(1 / sqrt(2), contentMode: .fit)
    }
}

struct CV_Previews: PreviewProvider {
    static var previews: some View {
        CV(info: cvInfoEnglish, style: cvStyle)
            .previewLayout(.fixed(width: 600, height: 600 * sqrt(2)))
    }
}
