//
//  SmallHeaderView.swift
//  Feels (iOS)
//
//  Created by Trey Tartt on 1/28/22.
//

import SwiftUI

struct SmallRollUpHeaderView: View {
    @Binding var viewType: MainSwitchableViewType
    
    let entries: [MoodEntry]
    private var moodMetrics = [MoodGroupingMetrics]()
    
    init(entries: [MoodEntry], viewType: Binding<MainSwitchableViewType>) {
        self.entries = entries
        self._viewType = viewType
        
        moodMetrics = Random.createTotalPerc(fromEntries: entries)
    }
    
    private func textView(forModel model: MoodGroupingMetrics) -> Text {
        switch viewType {
        case .total:
            return Text(String(model.total))
        case .percentageCircle:
            return  Text("\(model.percent, specifier: "%.0f")%")
        case .percentage:
            return  Text("\(model.percent, specifier: "%.0f")%")
        }
    }
    
    private var textViews: some View {
        HStack {
            ForEach(moodMetrics, id: \.0) { model in
                textView(forModel: model)
                    .font(.title2)
                    .fontWeight(.bold)
                    .lineLimit(1)
                    .foregroundColor(model.mood.color)
                    .frame(width: 70, height: 70)
            }
        }
        .padding([.top, .bottom])
    }
    
    private var circularViews: some View {
        HStack {
            ForEach(moodMetrics, id: \.0) { model in
                ZStack {
                    Circle().fill(model.mood.color)
                        .frame(minWidth: 5,
                               maxWidth: 70,
                               minHeight: 5,
                               maxHeight: 70,
                               alignment: .center)
                        .overlay(
                            textView(forModel: model)
                                .font(.title3)
                                .fontWeight(.bold)
                                .lineLimit(1)
                                .clipShape(ContainerRelativeShape()).padding()
                                .foregroundColor(Color(UIColor.white))
                                .minimumScaleFactor(0.1)
                            )
                }
            }
        }
        .padding([.top, .bottom])
    }
    
    private var viewOnViewtype : some View {
        HStack {
            switch viewType {
            case .total, .percentageCircle:
                circularViews
            case .percentage:
                textViews
            }
        }
    }
    
    var body: some View {
        viewOnViewtype
            .frame(maxWidth: .infinity, alignment: .leading)
    }
}

struct SmallHeaderView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10),
                                  viewType: .constant(.total))
            
            SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10),
                                  viewType: .constant(.total))
                .frame(height: 20)
        }
    }
}
