//
//  MuteAccountView.swift
//  Tusker
//
//  Created by Shadowfacts on 11/11/22.
//  Copyright © 2022 Shadowfacts. All rights reserved.
//

import SwiftUI
import Pachyderm
import TuskerComponents

struct MuteAccountView: View {
    private static let durationOptions: [MenuPicker<TimeInterval>.Option] = {
        let f = DateComponentsFormatter()
        f.maximumUnitCount = 1
        f.unitsStyle = .full
        f.allowedUnits = [.weekOfMonth, .day, .hour, .minute]
        
        let durations: [TimeInterval] = [
                     30 * 60,
                     60 * 60,
                 6 * 60 * 60,
                24 * 60 * 60,
            3 * 24 * 60 * 60,
            7 * 24 * 60 * 60,
        ]
        return [
            .init(value: 0, title: "Forever")
        ] + durations.map { .init(value: $0, title: f.string(from: $0)!) }
    }()
    
    let account: AccountMO
    let mastodonController: MastodonController
    
    @Environment(\.dismiss) private var dismiss: DismissAction
    @ObservedObject private var preferences = Preferences.shared
    @State private var muteNotifications = true
    @State private var duration: TimeInterval = 0
    @State private var isMuting = false
    @State private var error: Error?
    
    var body: some View {
        if #available(iOS 16.0, *) {
            NavigationStack {
                navigationViewContent
            }
        } else {
            NavigationView {
                navigationViewContent
            }
            .navigationViewStyle(.stack)
        }
    }
    
    private var navigationViewContent: some View {
        Form {
            Section {
                HStack {
                    AvatarImageView(
                        url: account.avatar,
                        size: 50,
                        style: Preferences.shared.avatarStyle == .circle ? .circle : .roundRect,
                        fetchAvatar: { await ImageCache.avatars.get($0).1 }
                    )
                    
                    VStack(alignment: .leading) {
                        AccountDisplayNameView(account: account, textStyle: .headline, emojiSize: 17)
                        Text("@\(account.acct)")
                            .fontWeight(.light)
                            .foregroundColor(.secondary)
                    }
                }
                .frame(height: 50)
                .listRowBackground(EmptyView())
                // vertical insets are so the rounded corners fo the section don't affect the avatar
                .listRowInsets(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0))
            }
            .accessibilityHidden(true)
            
            Section {
                Toggle(isOn: $muteNotifications) {
                    Text("Hide notifications from this person")
                }
            } footer: {
                if muteNotifications {
                    Text("This user's posts and notifications will be hidden.")
                } else {
                    Text("This user's posts will be hidden from your timeline. You can still receive notifications from them.")
                }
            }
            .appGroupedListRowBackground()
            
            Section {
                Picker(selection: $duration) {
                    ForEach(MuteAccountView.durationOptions, id: \.value) { option in
                        Text(option.title).tag(option.value)
                    }
                } label: {
                    Text("Duration")
                }
            } footer: {
                if duration != 0 {
                    Text("The mute will automatically be removed after the selected time.")
                }
            }
            .appGroupedListRowBackground()

            Button(action: self.mute) {
                if isMuting {
                    HStack {
                        Text("Muting User")
                        Spacer()
                        ProgressView()
                            .progressViewStyle(.circular)
                    }
                } else {
                    Text("Mute User")
                }
            }
            .disabled(isMuting)
            .appGroupedListRowBackground()
        }
        .appGroupedListBackground(container: UIHostingController<MuteAccountView>.self)
        .alertWithData("Erorr Muting", data: $error, actions: { error in
            Button("OK") {}
        }, message: { error in
            Text(error.localizedDescription)
        })
        .navigationTitle("Mute \(account.displayOrUserName)")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .cancellationAction) {
                Button("Cancel") {
                    self.dismiss()
                }
            }
        }
    }
    
    private func mute() {
        isMuting = true
        let req = Account.mute(account.id, notifications: muteNotifications)
        Task {
            do {
                let (relationship, _) = try await mastodonController.run(req)
                mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
                self.dismiss()
            } catch {
                self.error = error
                isMuting = false
            }
        }
    }
}

//struct MuteAccountView_Previews: PreviewProvider {
//    static var previews: some View {
//        MuteAccountView()
//    }
//}
