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

import SwiftUI

struct SmallRollUpHeaderView: View {
    @Binding var viewType: MainSwitchableViewType
    @AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
    @AppStorage(UserDefaultsStore.Keys.shape.rawValue, store: GroupUserDefaults.groupDefaults) private var shape: BGShape = .circle
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system

    private var textColor: Color { theme.currentTheme.labelColor }

    let entries: [MoodEntryModel]
    private var moodMetrics = [MoodMetrics]()

    init(entries: [MoodEntryModel], viewType: Binding<MainSwitchableViewType>) {
        self.entries = entries
        self._viewType = viewType

        moodMetrics = Random.createTotalPerc(fromEntries: entries)
    }
    
    private func textView(forModel model: MoodMetrics) -> Text {
        switch viewType {
        case .total:
            return Text(String(model.total))
        case .percentageShape:
            return  Text("\(model.percent, specifier: "%.0f")%")
        case .percentage:
            return  Text("\(model.percent, specifier: "%.0f")%")
        }
    }
    
    private var onlyTextView: some View {
        HStack() {
            ForEach(moodMetrics, id: \.id) { model in
                textView(forModel: model)
                    .font(.title2)
                    .fontWeight(.bold)
                    .lineLimit(1)
                    .minimumScaleFactor(0.5)
                    .foregroundColor(moodTint.color(forMood: model.mood))
                    .frame(maxWidth: .infinity, alignment: .center)
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding([.top, .bottom])
    }
    
    private var shapeView: some View {
        HStack {
            ForEach(moodMetrics, id: \.id) { model in
                HStack {
                    shape.view(withText: textView(forModel: model),
                               bgColor: moodTint.color(forMood: model.mood),
                               textColor: textColor)
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                }
            }
        }
        .padding([.top, .bottom])
    }
    
    private var viewOnViewtype : some View {
        HStack {
            switch viewType {
            case .total, .percentageShape:
                shapeView
            case .percentage:
                onlyTextView
            }
        }
    }
    
    var body: some View {
        viewOnViewtype
            .frame(maxWidth: .infinity, alignment: .leading)
    }
}

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

            SmallRollUpHeaderView(entries: DataController.shared.randomEntries(count: 10),
                                  viewType: .constant(.percentageShape))
                .background(.gray)

            SmallRollUpHeaderView(entries: DataController.shared.randomEntries(count: 10),
                                  viewType: .constant(.percentage))

                .background(.gray)
        }
    }
}
