//
//  HeaderStatsView.swift
//  Feels
//
//  Created by Trey Tartt on 1/8/22.
//

import SwiftUI
import Charts

struct HeaderStatsView : UIViewRepresentable {
    //Bar chart accepts data as array of BarChartDataEntry objects
    var entries : [BarChartDataEntry]
    
    init(fakeData: Bool, backDays: Int) {
        entries = [BarChartDataEntry]()
        
        var moodEntries: [MoodEntry]?

        if fakeData {
            moodEntries = PersistenceController.shared.randomEntries(count: 10)
        } else {
            var daysAgo = Calendar.current.date(byAdding: .day, value: -backDays, to: Date())!
            daysAgo = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: daysAgo)!
            
            moodEntries = PersistenceController.shared.getData(startDate: daysAgo, endDate: Date(), includedDays: [1,2,3,4,5,6,7])
        }
        if let moodEntries = moodEntries {
            for (index, mood) in Mood.allValues.enumerated() {
                entries.append(BarChartDataEntry(x: Double(index + 1),
                                                 y: Double(moodEntries.filter({
                    Int($0.moodValue) == mood.rawValue
                }).count)))
            }
        }
    }
    
    // this func is required to conform to UIViewRepresentable protocol
    func makeUIView(context: Context) -> BarChartView {
        //crate new chart
        let chart = BarChartView()
        chart.drawGridBackgroundEnabled = false
        chart.drawValueAboveBarEnabled = false

        chart.xAxis.drawAxisLineEnabled = false
        chart.xAxis.labelTextColor = .clear

        chart.rightAxis.drawAxisLineEnabled = false
        chart.rightAxis.labelTextColor = .clear

        chart.leftAxis.drawAxisLineEnabled = false
        chart.leftAxis.labelTextColor = .clear

        chart.xAxis.drawGridLinesEnabled = false
        chart.leftAxis.drawGridLinesEnabled = false
        chart.rightAxis.drawGridLinesEnabled = false

        chart.leftAxis.axisLineColor = .clear
        chart.rightAxis.axisLineColor = .clear

        chart.legend.textColor = .clear
        chart.legend.enabled = false

        chart.drawBordersEnabled = false
        chart.drawMarkers = false
        chart.borderColor = .clear

        chart.doubleTapToZoomEnabled = false
        chart.leftAxis.axisMinimum = 0
        
        let data = BarChartData()
        let dataSet = dataSet()
        data.append(dataSet)
        
        chart.data = data
        dataSet.valueFormatter = DefaultValueFormatter(decimals: 0)
        
        return chart
    }
    
    // this func is required to conform to UIViewRepresentable protocol
    func updateUIView(_ uiView: BarChartView, context: Context) {
        let data = BarChartData()
        let dataSet = dataSet()
        data.append(dataSet)
        uiView.data = data
        
        dataSet.valueFormatter = DefaultValueFormatter(decimals: 0)
    }
    
    func dataSet() -> BarChartDataSet {
        let dataSet = BarChartDataSet(entries: entries)

        // change bars color to green
        dataSet.colors = Mood.allValues.map({ NSUIColor( $0.color ) })
        dataSet.secondaryTextColor = UIColor.systemGray
        dataSet.valueColors = [.white]
        dataSet.highlightAlpha = 0.0
        
        if let descriptor = UIFontDescriptor.preferredFontDescriptor(
          withTextStyle: .body).withSymbolicTraits([.traitBold]) {
            dataSet.valueFont = UIFont(descriptor: descriptor, size: 0)
        } else {
            dataSet.valueFont = UIFont.preferredFont(forTextStyle: .body)
        }
        
        return dataSet
    }
    
    typealias UIViewType = BarChartView
    
}



struct HeaderStatsView_Previews: PreviewProvider {
    static var previews: some View {
        HeaderStatsView(fakeData: true, backDays: 30).frame(minHeight: 85, maxHeight: 90)
    }
}
