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

import SwiftUI

struct CurrentStreakTemplate: View, SharingTemplate {
    var image: UIImage {
        return  UIImage(systemName: "square.and.arrow.up")!
    }
    
    static var description: String {
        "Last 10 Days"
    }
    
    var isPreview: Bool
    var startDate: Date
    var endDate: Date
    let moodEntries: [MoodEntry]
    
    @State var showSharingTemplate = false
    @Environment(\.presentationMode) var presentationMode
    
    
    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)
    ]
    
    init(isPreview: Bool, startDate: Date, endDate: Date, fakeData: Bool) {
        self.isPreview = isPreview
        self.startDate = startDate
        self.endDate = endDate
        
        var _moodEntries: [MoodEntry]?
        
        if fakeData {
            _moodEntries = PersistenceController.shared.randomEntries(count: 10)
        } else {
            
            _moodEntries = PersistenceController.shared.getData(startDate:startDate,
                                                                endDate: endDate,
                                                                includedDays: [1,2,3,4,5,6,7])
        }
        
        self.moodEntries = _moodEntries ?? [MoodEntry]()
    }
    
    var preview: some View {
        HStack {
            VStack {
                LazyVGrid(columns: columns, spacing: 0) {
                    ForEach(moodEntries) { entry in
                        entry.mood.icon
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .foregroundColor(entry.mood.color)
                    }
                }
            }
        }
        .frame(height: 100)
    }
    
    var shareView: some View {
        VStack {
            Text(String(format: String(localized: "share_view_current_streak_template_title")))
                .font(.title)
                .foregroundColor(Color(UIColor.label))
                .frame(maxWidth: .infinity, alignment: .center)
                .padding(.top)
            
            HStack {
                VStack {
                    LazyVGrid(columns: columns, spacing: 0) {
                        ForEach(moodEntries) { entry in
                            entry.mood.icon
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .foregroundColor(entry.mood.color)
                        }
                    }
                }
            }
            .padding()
        }
    }
    
    var mainView: some View {
        VStack {
            shareView
                .background(Color(UIColor.secondarySystemBackground))
                .cornerRadius(10)
            Spacer()
            HStack(alignment: .center) {
                Button(action: {
                    showSharingTemplate = true
                }, label: {
                    Text("Share")
                        .font(.title)
                        .fontWeight(.bold)
                        .foregroundColor(Color.white)
                        .padding(.top, 20)
                })
                    .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)
        }.sheet(isPresented: $showSharingTemplate) {
            ActivityViewController(activityItems: [self.image])
        }
    }
    
    var body: some View {
        if isPreview {
            preview
                .padding([.leading, .trailing], -20)
        } else {
            mainView
                .padding([.leading, .trailing])
        }
    }
}

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)
    }
}
