//
//  CurrentStreakTemplate.swift
//  Reflect (iOS)
//
//  Created by Trey Tartt on 2/9/22.
//

import SwiftUI

struct CurrentStreakTemplate: View, SharingTemplate {
    static var description: String {
        "Last 10 Days"
    }

    var isPreview: Bool
    var startDate: Date
    var endDate: Date
    let moodEntries: [MoodEntryModel]

    @State var showSharingTemplate = false
    @StateObject private var shareImage = ShareImageStateViewModel()

    @Environment(\.presentationMode) var presentationMode
    @AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
    @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system

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

    let columns = [
        GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
        GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
        GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
        GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center),
        GridItem(.flexible(minimum: 5, maximum: .infinity), alignment: .center)
    ]

    @MainActor
    init(isPreview: Bool, startDate: Date, endDate: Date, fakeData: Bool) {
        self.isPreview = isPreview
        self.startDate = startDate
        self.endDate = endDate

        var _moodEntries: [MoodEntryModel]?

        if fakeData {
            _moodEntries = DataController.shared.randomEntries(count: 10)
        } else {
            _moodEntries = DataController.shared.getData(startDate:startDate,
                                                         endDate: endDate,
                                                         includedDays: [1,2,3,4,5,6,7])
        }

        self.moodEntries = _moodEntries ?? [MoodEntryModel]()
    }
    
    var image: UIImage {
        let image = shareView.asImage(size: CGSize(width: 666, height: 1190))
        return image
    }
    
    var preview: some View {
        HStack {
            VStack {
                LazyVGrid(columns: columns, spacing: 0) {
                    ForEach(moodEntries) { entry in
                        entry.mood.icon
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .foregroundColor(moodTint.color(forMood: entry.mood))
                    }
                }
            }
        }
        .frame(height: 100)
    }
    
    var shareView: some View {
        VStack {
            Text(String(format: String(localized: "share_view_current_streak_template_title")))
                .font(.title)
                .foregroundColor(textColor)
                .frame(maxWidth: .infinity, alignment: .center)
                .padding(.top)
            
            HStack {
                VStack {
                    LazyVGrid(columns: columns, spacing: 10) {
                        ForEach(moodEntries) { entry in
                            entry.mood.icon
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .foregroundColor(moodTint.color(forMood: entry.mood))
                        }
                    }
                }
            }
            .padding()
        }
        .background(Color(UIColor.secondarySystemBackground))
        .cornerRadius(10)
        .padding()
    }
    
    var mainView: some View {
        VStack {
            shareView
                
            Spacer()
            HStack(alignment: .center) {
                HStack(alignment: .center) {
                    Button(action: {
                        let _image = self.image
                        self.shareImage.showSheet = true
                        self.shareImage.selectedShareImage = _image
                    }, label: {
                        Text("Share")
                            .font(.title)
                            .fontWeight(.bold)
                            .foregroundColor(Color.white)
                            .padding(.top, 20)
                    })
                        .sheet(isPresented: self.$shareImage.showSheet) {
                            if let uiImage = self.shareImage.selectedShareImage {
                                ShareSheet(photo: uiImage)
                            }
                        }
                        .frame(maxWidth: .infinity, alignment: .center)
                        .background(
                            Color.green
                        )
                        .padding(.trailing, -5)
                
                Button(action: {
                    presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Exit")
                        .font(.title)
                        .fontWeight(.bold)
                        .foregroundColor(Color.white)
                        .padding(.top, 20)
                })
                    .frame(maxWidth: .infinity, alignment: .center)
                    .background(
                        Color.red
                    )
                    .padding(.leading, -5)
            }
            .padding([.leading, .trailing], -20)
        }
    }
    }
    
    var body: some View {
        if isPreview {
            shareView
                .scaleEffect(2)
        } else {
            mainView
        }
    }
}

struct CurrentStreakTemplate_Previews: PreviewProvider {
    static var previews: some View {
        CurrentStreakTemplate(isPreview: true, startDate: Date(), endDate: Date(), fakeData: true)
        
        CurrentStreakTemplate(isPreview: false, startDate: Date(), endDate: Date(), fakeData: true)
        
        CurrentStreakTemplate(isPreview: false, startDate: Date(), endDate: Date(), fakeData: true).shareView
    }
}
