MapsViewController.swift 5.66 KB
//
//  MapsViewController.swift
//  SwiftWarplyFramework
//
//  Created by Βασιλης Σκουρας on 14/9/22.
//

import UIKit
import MapKit

class MapsViewController: UIViewController, MKMapViewDelegate {

    // ui
    @IBOutlet private var mapView: MKMapView!
    
    @IBOutlet private var titleView: UILabel!
    @IBOutlet private var nameImageView: UIImageView!
    @IBOutlet private var nameView: UILabel!
    @IBOutlet private var hoursView: UILabel!
    @IBOutlet private var phoneView: UILabel!
    @IBOutlet private var addressView: UILabel!
    @IBOutlet private var directionsButton: UIButton!
    @IBOutlet private var infoTopConstraint: NSLayoutConstraint!
    
    // public
    public var couponSet: swiftApi.CouponSetItemModel?
    var merchantsArray:Array<swiftApi.MerchantModel> = []
    
    //
    var loading: Bool = false
    let initialLocation = CLLocation(latitude: 38.0689416, longitude: 19.9906012) // greece

    // 
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hidesBottomBarWhenPushed = true
        
        self.infoTopConstraint.constant = 0
        
        setBackButton("ic_close")
        //setNavigationTitle(ccms?._titleOffer ?? "")
        
        mapView.delegate = self
        mapView.centerToLocation(initialLocation)
        
        //titleView.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
        //titleView.setTitleColor(.white, for: .normal)
        
        //nameView.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
        //nameView.setTitleColor(.white, for: .normal)
        
        //hoursView.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
        //hoursView.setTitleColor(.white, for: .normal)
        
        //phoneView.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
        //phoneView.setTitleColor(.white, for: .normal)
        
        //addressView.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
        //addressView.setTitleColor(.white, for: .normal)
        
        //directionsButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
        directionsButton.setTitle("Οδηγίες", for: .normal)
        directionsButton.setTitleColor(.white, for: .normal)
        directionsButton.backgroundColor = UIColor(red: 0.47, green: 0.75, blue: 0.08, alpha: 1.00)
        directionsButton.layer.cornerRadius = 12.0
    }
    
    // mvp
    @objc func load() {
    
        if (loading) {
            return;
        }

        showLoading()
        
        // TODO: adapt params
        let merchantUuid: String = couponSet!.merchant_uuid!
        swiftApi().getMultilingualMerchantsAsync([], false, 0.0, [], "", 0, [merchantUuid], getMerchantsCallback)
        showContent()
    }
    
    func getMerchantsCallback (_ merchantsData: Array<swiftApi.MerchantModel>?) -> Void {
        if (merchantsData != nil) {
            DispatchQueue.main.async {
                self.merchantsArray = merchantsData!
                self.loadMapPins()
            }
            
            return
        }
    }
    
    private func showLoading() {
        
        loading = true
    }
    
    private func showError() {
    }
    
    private func showContent() {
        
        loading = false
    }
    
    // private
    @IBAction func closeButtomAction(_ sender: Any) {
        hidePinDetailsView()
    }
    
    private func loadMapPins() {
        for item in merchantsArray {
            let pin = MKPointAnnotation()
            pin.coordinate = CLLocationCoordinate2D(latitude: item._latitude, longitude: item._longitude)
            mapView.addAnnotation(pin)
        }
    }
    
    private func loadPinDetailsView() {
        self.titleView.text = "INTERSPORT"
        self.nameImageView.image = UIImage(named: "intersport")
        self.nameView.text = "Intersport Χαλάνδρι"
        self.hoursView.text = "Δευτέρα - Τετάρτη"
        self.phoneView.text = "210-6230453"
        self.addressView.text = "ΕΘΝΙΚΗΣ ΑΝΤΙΣΤΑΣΕΩΝ 4 41"
    }
    
    private func showPinDetailsView() {
        self.infoTopConstraint.constant = -320
        UIView.animate(withDuration: 2.0) {
            self.view.layoutIfNeeded()
        }
    }
    
    private func hidePinDetailsView() {
        self.infoTopConstraint.constant = 0
        UIView.animate(withDuration: 2.0) {
            self.view.layoutIfNeeded()
        }
    }
    
    // map view delegate
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }
        
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "warply_custom")
        if (annotationView == nil) {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "warply_custom")
            annotationView?.canShowCallout = false
        } else {
            annotationView?.annotation = annotation
        }
        
        // TODO: add custom pin image?
        annotationView?.image = UIImage(named: "custom")
        
        return annotationView
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        loadPinDetailsView()
        showPinDetailsView()
    }

    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
        hidePinDetailsView()
    }
}

private extension MKMapView {
    
  func centerToLocation(_ location: CLLocation, regionRadius: CLLocationDistance = 1000) {
    let coordinateRegion = MKCoordinateRegion(
      center: location.coordinate,
      latitudinalMeters: regionRadius,
      longitudinalMeters: regionRadius)
    setRegion(coordinateRegion, animated: true)
  }
}