//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import SwiftUI

enum RoomTimelineViewProvider: Identifiable, Hashable {
    case text(TextRoomTimelineItem)
    case separator(SeparatorRoomTimelineItem)
    case image(ImageRoomTimelineItem)
    case video(VideoRoomTimelineItem)
    case file(FileRoomTimelineItem)
    case emote(EmoteRoomTimelineItem)
    case notice(NoticeRoomTimelineItem)
    case redacted(RedactedRoomTimelineItem)
    case encrypted(EncryptedRoomTimelineItem)
    case readMarker(ReadMarkerRoomTimelineItem)
    case paginationIndicator(PaginationIndicatorRoomTimelineItem)
    case sticker(StickerRoomTimelineItem)
    case unsupported(UnsupportedRoomTimelineItem)
    case timelineStart(TimelineStartRoomTimelineItem)
    case state(StateRoomTimelineItem)
    
    var id: String {
        switch self {
        case .text(let item):
            return item.id
        case .separator(let item):
            return item.id
        case .image(let item):
            return item.id
        case .video(let item):
            return item.id
        case .file(let item):
            return item.id
        case .emote(let item):
            return item.id
        case .notice(let item):
            return item.id
        case .redacted(let item):
            return item.id
        case .encrypted(let item):
            return item.id
        case .readMarker(let item):
            return item.id
        case .paginationIndicator(let item):
            return item.id
        case .sticker(let item):
            return item.id
        case .unsupported(let item):
            return item.id
        case .timelineStart(let item):
            return item.id
        case .state(let item):
            return item.id
        }
    }
    
    /// Whether or not it is possible to send a reaction to this timeline item.
    var isReactable: Bool {
        switch self {
        case .text, .image, .video, .file, .emote, .notice, .sticker:
            return true
        case .redacted, .encrypted, .unsupported, .state: // Event based items that aren't reactable
            return false
        case .timelineStart, .separator, .readMarker, .paginationIndicator: // Virtual items are never reactable
            return false
        }
    }
}

extension RoomTimelineViewProvider: View {
    @ViewBuilder var body: some View {
        switch self {
        case .text(let item):
            TextRoomTimelineView(timelineItem: item)
        case .separator(let item):
            SeparatorRoomTimelineView(timelineItem: item)
        case .image(let item):
            ImageRoomTimelineView(timelineItem: item)
        case .video(let item):
            VideoRoomTimelineView(timelineItem: item)
        case .file(let item):
            FileRoomTimelineView(timelineItem: item)
        case .emote(let item):
            EmoteRoomTimelineView(timelineItem: item)
        case .notice(let item):
            NoticeRoomTimelineView(timelineItem: item)
        case .redacted(let item):
            RedactedRoomTimelineView(timelineItem: item)
        case .encrypted(let item):
            EncryptedRoomTimelineView(timelineItem: item)
        case .readMarker(let item):
            ReadMarkerRoomTimelineView(timelineItem: item)
        case .paginationIndicator(let item):
            PaginationIndicatorRoomTimelineView(timelineItem: item)
        case .sticker(let item):
            StickerRoomTimelineView(timelineItem: item)
        case .unsupported(let item):
            UnsupportedRoomTimelineView(timelineItem: item)
        case .timelineStart(let item):
            TimelineStartRoomTimelineView(timelineItem: item)
        case .state(let item):
            StateRoomTimelineView(timelineItem: item)
        }
    }
}
