//
// 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 SwiftUI

struct MapCreditsActionSheet {
    // bwi #5379 dynamic attribution from style.json
    let attribution: LocationSharingAttribution
    
    // Open URL action
    let openURL: (URL) -> Void
    
    // Map credits action sheet
    var sheet: ActionSheet {
        ActionSheet(title: Text(BWIL10n.locationSharingMapCreditsTitle),
                    buttons: self.creditButtons(attribution: attribution))
    }
    
    func creditButtons( attribution: LocationSharingAttribution) -> [ActionSheet.Button] {
        
        var buttons = [ActionSheet.Button]()
        
        // bwi #5379 a bit scetchy but you can asume that url and text have the same index
        for (index, copyright) in attribution.copyrightTexts.enumerated() {
            buttons.append(.default(Text(copyright)) {
                if let url = attribution.copyrightLinks[index] {
                    openURL(url)
                }
            })
        }
        buttons.append(.cancel())
        
        return buttons
    }
}
