//
//  ViewController.swift
//  CD-StackViews
//
//  Created by bulko on 3/13/26.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var displayButton: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    
    var current = 0     // current image number
    let imageCount = 6  // numnber of images
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        current = 0
        showImage(index: current)
        
        let actions = [
            UIAction(title: "Image 0") { _ in self.showImage(index: 0) },
            UIAction(title: "Image 1") { _ in self.showImage(index: 1) },
            UIAction(title: "Image 2") { _ in self.showImage(index: 2) },
            UIAction(title: "Image 3") { _ in self.showImage(index: 3) },
            UIAction(title: "Image 4") { _ in self.showImage(index: 4) },
            UIAction(title: "Image 5") { _ in self.showImage(index: 5) }
        ]
        
        let menu = UIMenu(title: "My Menu", options: .displayInline, children: actions)
        displayButton.menu = menu
        displayButton.showsMenuAsPrimaryAction = true

    }
    
    func showImage(index: Int) {
        displayButton.setTitle("Image \(index)", for: .normal)
        imageView.image = UIImage(named: "image\(index)")
        
    }
   
    @IBAction func onPreviousButtonPressed(_ sender: Any) {
        current = (current - 1 + imageCount) % imageCount
        showImage(index: current)
    }
    
    @IBAction func onResetButtonPressed(_ sender: Any) {
        current = 0
        showImage(index: current)
    }
    
    @IBAction func onNextButtonPressed(_ sender: Any) {
        current = (current + 1) % imageCount
        showImage(index: current)
    }
    
}

