UnifiedCouponBarcodeViewController.swift 14.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
//
//  UnifiedCouponBarcodeViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 6/4/23.
//

import UIKit
import RSBarcodes_Swift
import AVFoundation
import SwiftEventBus

@objc public class UnifiedCouponBarcodeViewController: UIViewController {
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var tableView: UITableView!
    
    let uiscreen: CGRect = UIScreen.main.bounds
    
    var termsVisible: Bool = false;
    var barcodeVisible: Bool = false;
    var couponsVisible: Bool = false;

    var mapBtnVisible: Bool = false;
    var eshopBtnVisible: Bool = false;
    var eshopWebsite: String = "";
    
    public var coupon: swiftApi.UnifiedCouponModel?
    public var isFromWallet: Bool? = false
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hidesBottomBarWhenPushed = true
        
        tableView.delegate = self
        tableView.dataSource = self

        // Do any additional setup after loading the view.
        setBackButton()
        setNavigationTitle("Εκπτωτικό κουπόνι")

        // Logs
//        print("Coupon: " + (coupon?.coupon ?? ""))
//        print("Coupon Name: " + (couponSetData?.name ?? ""))
//        print("Coupon Description: " + (couponSetData?.short_description ?? ""))
//        print("Coupon Expiration: " + (coupon?.expiration ?? ""))
        
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        swiftApi().logTrackersEvent("screen", "UnifiedCouponScreen")
        
        self.navigationController?.hideHairline()
    }
    
    
    // MARK: - Functions
    func showConfirmDialog() -> Void {

        let alert = UIAlertController(title: "Ακύρωση Κουπονιού", message: "Είσαι σίγουρος ότι θέλεις να ακυρώσεις το κουπόνι σου;", preferredStyle: .alert)

        let cancelButton = UIAlertAction(title: "Όχι", style: .default, handler: { action in
            switch action.style{
                case .default:
                print("default")

                case .cancel:
                print("cancel")

                case .destructive:
                print("destructive")

            }
        })
        // cancelButton.setValue(UIColor(rgb: 0xFC5757), forKey: "titleTextColor")
        alert.addAction(cancelButton)

        alert.addAction(UIAlertAction(title: "Ναι", style: .default, handler: { action in
            switch action.style{
                case .default:
                self.cancelUnifiedCouponRequest()

                case .cancel:
                print("cancel")

                case .destructive:
                print("destructive")

            }
        }))
        
        self.present(alert, animated: true, completion: nil)
    }

    func showSuccessDialog() -> Void {

        let alert = UIAlertController(title: "Ακύρωση Κουπονιού", message: "Το ενιαίο κουπόνι σου ακυρώθηκε επιτυχώς. Βρες τα μεμονωμένα κουπόνια στο καλάθι SUPERMARKET DEALS.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
            switch action.style{
                case .default:
                SwiftEventBus.post("refresh_unified_coupons")
                self.navigationController?.popViewController(animated: true)
                // self.dismiss(animated: true, completion: {})

                case .cancel:
                print("cancel")

                case .destructive:
                print("destructive")

            }
        }))
        self.present(alert, animated: true, completion: nil)
    }
    
    func cancelUnifiedCouponRequest () -> Void {
        var tranIdFound = "";
        for coupon in self.coupon?._coupons ?? [] {
            if ((coupon.transaction_uuid != nil) && coupon.transaction_uuid != "") {
                tranIdFound = coupon.transaction_uuid ?? "";
                break;
            }
        }
        
        if (tranIdFound != "") {
            swiftApi().cancelUnifiedCouponAsync(transactionId: tranIdFound, self.cancelUnifiedCouponCallback, failureCallback: { errorCode in })
        }
    }
    
    func cancelUnifiedCouponCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void {
        if (responseData != nil) {
            DispatchQueue.main.async {
                if (responseData?.getStatus == 1) {
                    self.showSuccessDialog()
                    
                    swiftApi().getUnifiedCouponsDeals { unifiedCoupons in
                    } failureCallback: { errorCode in
                    }
                }
                
            }
        }
    }
    
    // MARK: - Actions
    @IBAction func showCouponsButtonAction(_ sender: Any) {
        couponsVisible = !couponsVisible
//        self.tableView.reloadSections(IndexSet(0..<2), with: .none)
        self.tableView.reloadData()
    }
    
    @IBAction func showBarcodeAction(_ sender: Any) {
        barcodeVisible = !barcodeVisible
//        self.tableView.reloadSections(IndexSet(integer: 0), with: .none)
        self.tableView.reloadData()
    }
    
    @IBAction func termsButtonAction(_ sender: Any) {
        termsVisible = !termsVisible
//        self.tableView.reloadSections(IndexSet(integer: 2), with: .none)
        self.tableView.reloadData()
    }

    @IBAction func mapButtonAction(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
        if let popupVC = storyboard.instantiateViewController(withIdentifier: "PopupMerchantCategoriesViewController") as? SwiftWarplyFramework.PopupMerchantCategoriesViewController {
            popupVC.modalPresentationStyle = .overCurrentContext
            popupVC.modalTransitionStyle = .crossDissolve
//            popupVC.coupon = coupon
            popupVC.delegate = self
            
            self.present(popupVC, animated: true, completion: nil)
        }
        
        // =======
        
//        swiftApi().logTrackersEvent("click", "SeeShops")
//
//        let vc = SwiftWarplyFramework.MapsViewController(nibName: "MapsViewController", bundle: Bundle(for: MyEmptyClass.self))
////        vc.couponSet = coupon?.couponset_data
//        vc.isMarket = true
//        self.navigationController?.pushViewController(vc, animated: true)

        // =======
        
        // if (mapBtnVisible == true) {
        //     swiftApi().logTrackersEvent("click", "SeeShops")
            
        //     let vc = SwiftWarplyFramework.MapsViewController(nibName: "MapsViewController", bundle: Bundle(for: MyEmptyClass.self))
        //     vc.couponSet = coupon?.couponset_data
        //     self.navigationController?.pushViewController(vc, animated: true)

        // } else if (eshopBtnVisible == true) {
        //     if (eshopWebsite != "") {
        //         guard let websiteUrl = URL(string: eshopWebsite) else {
        //               print("Error creating URL")
        //               return
        //         }
                
        //         // check if link can be opened.
        //         guard UIApplication.shared.canOpenURL(websiteUrl) else {
        //             return
        //         }
                
        //         swiftApi().logTrackersEvent("click", "SeeShopWebsite")
        //         UIApplication.shared.open(websiteUrl, options: [:], completionHandler: nil)
        //     }
        // }
    }
    
    @IBAction func cancelButtonAction(_ sender: Any) {
        self.showConfirmDialog()
    }
}

// MARK: - TableView
extension UnifiedCouponBarcodeViewController: UITableViewDelegate, UITableViewDataSource{
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return 1
        } else if (section == 1) {
            if (couponsVisible == true) {
                return self.coupon?._coupons.count ?? 0
            } else {
                return 0
            }
            
        } else if (section == 2) {
            return 1
        } else {
            return 0
        }

    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (indexPath.section == 0) {
            return UITableView.automaticDimension
        } else if (indexPath.section == 1) {
            if (couponsVisible == true) {
                return 130.0 + 8.0
            } else {
                return 0.0
            }
            
        } else if (indexPath.section == 2) {
            return UITableView.automaticDimension
        } else {
            return 0.0
        }
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "UnifiedCouponBCHeaderTableViewCell", for: indexPath) as! UnifiedCouponBCHeaderTableViewCell
            
            cell.configureCell(coupon: self.coupon, couponsVisible: couponsVisible)
            
            return cell
            
        } else if (indexPath.section == 1) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "CouponsTableViewCellId", for: indexPath) as! CouponsTableViewCell
            
            cell.configureCell(coupon: (self.coupon?._coupons[indexPath.row])!, isMarket: true)
            
            return cell
            
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "UnifiedCouponBCFooterTableViewCell", for: indexPath) as! UnifiedCouponBCFooterTableViewCell
            
            cell.configureCell(termsVisible: termsVisible)
            
            return cell
        }
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // SwiftEventBus.post("couponBarcodePressed", sender: coupons[indexPath.row])
        
        // Logs
//        let couponSetData: swiftApi.CouponSetItemModel? = self.coupon?._coupons[indexPath.row].couponset_data
//        print("Coupon clicked: " + (self.coupon?._coupons[indexPath.row].coupon ?? ""))
//        print("Coupon Name clicked: " + (couponSetData?.name ?? ""))
//        print("Coupon Description clicked: " + (couponSetData?.short_description ?? ""))
//        print("Coupon Expiration clicked: " + (self.coupon?._coupons[indexPath.row].expiration ?? ""))
        
        if (indexPath.section == 1) {
            swiftApi().logTrackersEvent("click", ("Coupon:" + (self.coupon?._coupons[indexPath.row].name ?? "")))
            
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
            let vc = storyboard.instantiateViewController(withIdentifier: "CouponBarcodeViewController") as! SwiftWarplyFramework.CouponBarcodeViewController
            vc.coupon = self.coupon?._coupons[indexPath.row]
            vc.isMarket = true
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if (section == 0){
            return nil
            
        } else if (section == 1) {
            if (couponsVisible == true) {
                let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 12))
                 view.backgroundColor =  UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)

                return view
            } else {
                return nil
            }
            
        } else {
            return nil
        }
    }

    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0) {
            return 0.0
            
        } else if (section == 1) {
            if (couponsVisible == true) {
                return 12.0
            } else {
                return 0.0
            }
            
        } else {
            return 0.0
        }
    }

    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if (section == 0) {
            return 0.0
            
        } else if (section == 1) {
            if (couponsVisible == true) {
                return 2.0
            } else {
                return 0.0
            }
            
        } else {
            return 0.0
        }
    }

    public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if (section == 0){
            return nil
            
        } else if (section == 1) {
            if (couponsVisible == true) {
                let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 2))
                 view.backgroundColor =  UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)

                return view
            } else {
                return nil
            }
            
        } else {
            return nil
        }
    }
    
}

// MARK: - MerchantTableViewCellDelegate
extension UnifiedCouponBarcodeViewController: PopupMerchantCategoriesViewControllerDelegate {
    
    func didTapMapButton() {
        swiftApi().logTrackersEvent("click", "SeeShops")

        let vc = SwiftWarplyFramework.MapsViewController(nibName: "MapsViewController", bundle: Bundle(for: MyEmptyClass.self))
//        vc.couponSet = coupon?.couponset_data
        vc.isMarket = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func didTapEshopsButton() {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
        if let popupVC = storyboard.instantiateViewController(withIdentifier: "PopupMerchantsViewController") as? SwiftWarplyFramework.PopupMerchantsViewController {
            popupVC.modalPresentationStyle = .overCurrentContext
            popupVC.modalTransitionStyle = .crossDissolve
            popupVC.availableShopsList = []
            popupVC.headerText = "Δες τα eshop"
            popupVC.buttonTitle = "Δες το eshop"
//            popupVC.buttonIcon = "shopping_cart"
            popupVC.footerText = "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία."
            popupVC.showEshops = true

            self.present(popupVC, animated: true, completion: nil)
        }
    }
    
}