swiftApi.swift 13.4 KB
//
//  swiftApi.swift
//  WarplySDKFrameworkIOS
//
//  Created by Βασιλης Σκουρας on 21/4/22.
//

import Foundation
import SwiftUI

public class swiftApi {

    public init() {
        
    }
    
    public func getUserTag() -> String {
        return "1"
    }
    
    public class DFY {
        let couponCode: String?
        let merchantId: String?
        
        init(couponCode: String, merchantId: String) {
            self.couponCode = couponCode
            self.merchantId = merchantId
        }
    }
    
    public func setDFY(couponCode: String, merchantId: String) {
        DFY.init(couponCode: couponCode, merchantId: merchantId)
    }

    public class activeDFYCoupons {
        let campaignIds: Array<String>
        
        init(campaignIds: Array<String>) {
            self.campaignIds = campaignIds
        }
    }
    
    public func setActiveDFYCoupons(campaignIds: Array<String>) {
        activeDFYCoupons.init(campaignIds: campaignIds)
    }
    
    public class CCMSLoyaltyCampaigns {
        let campaigns: Array<Dictionary<String, String>>
        
        init(campaigns: Array<Dictionary<String, String>>) {
            self.campaigns = campaigns
        }
    }
    
    public func setCCMSLoyaltyCampaigns(campaigns: Array<Dictionary<String, String>>) {
        CCMSLoyaltyCampaigns.init(campaigns: campaigns)
    }
    
    public func getActiveDFYCoupons() -> Array<String>{
        let array: Array<String> = []
        return array
    }

    
    public class CouponSetItemModel {
        let uuid: String?
        let admin_name: String?
        let name: String?
        let img_preview: String?
        let expiration: String?
        let description: String?
        let short_description: String?
        let discount: String?
        let sorting: Int?
        let inner_text: String?
        let buyable: Bool?
        let visible: Bool?
        
        init(dictionary: [String: Any]) {
            self.uuid = dictionary["uuid"] as? String? ?? ""
            self.admin_name = dictionary["admin_name"] as? String? ?? ""
            self.name = dictionary["name"] as? String? ?? ""
            self.img_preview = dictionary["img_preview"] as? String? ?? ""
            self.description = dictionary["description"] as? String? ?? ""
            self.short_description = dictionary["short_description"] as? String? ?? ""
            self.discount = dictionary["discount"] as? String? ?? ""
            self.sorting = dictionary["sorting"] as? Int? ?? nil
            self.inner_text = dictionary["inner_text"] as? String? ?? ""
            self.buyable = dictionary["buyable"] as? Bool? ?? false
            self.visible = dictionary["visible"] as? Bool? ?? false
            
            let expirationObject = dictionary["expiration"] as? [String: Any]? ?? ["":""]
            let expirationString = expirationObject?["value"] as? String? ?? ""
            
    //      Example expirationString: Optional(2022-12-05 01:55)
            
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd hh:mm"
            if let date = dateFormatter.date(from: expirationString ?? "") {
                dateFormatter.dateFormat = "dd/MM/yyyy"
                let resultString = dateFormatter.string(from: date)
                self.expiration = resultString
            } else {
                self.expiration = ""
            }
            
            
        }
        
//        var asDictionary : [String:Any] {
//          let mirror = Mirror(reflecting: self)
//          let dict = Dictionary(uniqueKeysWithValues: mirror.children.lazy.map({ (label:String?, value:Any) -> (String, Any)? in
//            guard let label = label else { return nil }
//            return (label, value)
//          }).compactMap { $0 })
//          return dict
//        }
    }

    public class CouponSetsDataModel {
        var data: Array<CouponSetItemModel> = []

        init() { //initializer method
            let instanceOfMyApi = MyApi()
            let couponSets = instanceOfMyApi.getCouponSets(withActive: true, andVisible: true, andUuids: nil)
            var couponSetsArray:Array<CouponSetItemModel> = []
            
            if let myCouponsSetsDictionary = couponSets as? [String : AnyObject] {
                let couponSetsData = (myCouponsSetsDictionary["MAPP_COUPON"] as! NSArray)
                
                
                for couponset in couponSetsData {
                    let tempCouponset = CouponSetItemModel(dictionary: couponset as! [String : Any])
                    couponSetsArray.append(tempCouponset)
                }
                
            }
            self.data = couponSetsArray
        }

        var getData: Array<CouponSetItemModel> {
            get { // getter
                return data
            }
        }
    }
    
    public func getCouponSets() -> Array<CouponSetItemModel> {
        return CouponSetsDataModel().getData
    }
    
    
    public class CouponItemModel {
        let couponset_uuid: String?
        let name: String?
        let image: String?
        let expiration: String?
        let description: String?
        let discount: String?
        let coupon: String?
        let category: String?
        let barcode: String?
        let status: Int?
        var couponset_data: CouponSetItemModel?
        
        init(dictionary: [String: Any]) {
            self.couponset_uuid = dictionary["couponset_uuid"] as? String? ?? ""
            self.name = dictionary["name"] as? String? ?? ""
            self.image = dictionary["image"] as? String? ?? ""
            self.description = dictionary["description"] as? String? ?? ""
            self.discount = dictionary["discount"] as? String? ?? ""
            self.coupon = dictionary["coupon"] as? String? ?? ""
            self.category = dictionary["category"] as? String? ?? ""
            self.barcode = dictionary["barcode"] as? String? ?? ""
            self.status = dictionary["status"] as? Int? ?? nil
            
            if let couponSetData = dictionary["couponset_data"] as? [String: Any]? ?? ["":""] {
                
                let tempCouponset = CouponSetItemModel(dictionary: couponSetData)
                
                self.couponset_data = tempCouponset
            } else {
                self.couponset_data = nil
            }
            
            
            let expirationString = dictionary["expiration"] as? String? ?? ""
            
    //      Example expirationString: Optional(2022-12-05 01:55)
            
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
            if let date = dateFormatter.date(from: expirationString ?? "") {
                dateFormatter.dateFormat = "dd/MM/yyyy"
                let resultString = dateFormatter.string(from: date)
                self.expiration = resultString
            } else {
                self.expiration = ""
            }
            
            
        }
        
        
        public func setCouponSetData(_ couponSet: CouponSetItemModel) {
            self.couponset_data = couponSet
        }
        
//        var asDictionary : [String:Any] {
//          let mirror = Mirror(reflecting: self)
//          let dict = Dictionary(uniqueKeysWithValues: mirror.children.lazy.map({ (label:String?, value:Any) -> (String, Any)? in
//            guard let label = label else { return nil }
//            return (label, value)
//          }).compactMap { $0 })
//          return dict
//        }
    }

    public class CouponsDataModel {
        var data: Array<CouponItemModel> = []

        init() { //initializer method
            let instanceOfMyApi = MyApi()
            let coupons = instanceOfMyApi.getCoupons()
            let couponSets = instanceOfMyApi.getCouponSets(withActive: true, andVisible: true, andUuids: nil)
            
            var couponsArray:Array<CouponItemModel> = []
            
            
            if let myCouponsSetsDictionary = couponSets as? [String : AnyObject] {
                let couponSetsData = (myCouponsSetsDictionary["MAPP_COUPON"] as! Array<NSMutableDictionary>)
                if let myCouponsDictionary = coupons as? [String : AnyObject] {
                    let couponsData = (myCouponsDictionary["result"] as! Array<NSMutableDictionary>)
                    if let sets = couponSetsData as? NSArray {
                        for set in sets {
                            let s = set as! NSDictionary
                            if let cpns = couponsData as? NSArray {
                                for coupon in cpns {
                                    var c = coupon as! NSDictionary
    //                                var temp = NSMutableDictionary(dictionary: s);
                                    if c["couponset_uuid"] as! String == s["uuid"] as! String {
                                        let temp = NSMutableDictionary(dictionary: c);
    
                                        temp.setValue(s as! [AnyHashable : Any],forKey: "couponset_data")
    
                                        let tempCoupon = CouponItemModel(dictionary: temp as! [String : Any])
                                        couponsArray.append(tempCoupon)
                                    }
    
                                }
                            }
    
                        }
                    }
                }
            }
            
//            if let myCouponsDictionary = coupons as? [String : AnyObject] {
//                let couponsData = (myCouponsDictionary["result"] as! NSArray)
//
//
//                for coupon in couponsData {
//                    let tempCoupon = CouponItemModel(dictionary: coupon as! [String : Any])
//                    couponsArray.append(tempCoupon)
//                }
//
//            }
            self.data = couponsArray
        }

        var getData: Array<CouponItemModel> {
            get { // getter
                return data.filter({
                    return $0.status == 1
                })
            }
        }
        
        
        var getOldCoupons: Array<CouponItemModel> {
            get { // getter
                return data.filter({
                    return $0.status != 1
                })
            }
        }
        
    }
    
    public func getCoupons() -> Array<CouponItemModel> {
        return CouponsDataModel().getData
    }
    
    public func getOldCoupons() -> Array<CouponItemModel> {
        return CouponsDataModel().getOldCoupons
    }
    
    public class CampaignItemModel {
        let index_url: String?
        let logo_url: String?
        let offer_category: String?
        let title: String?
        let subtitle: String?
        let session_uuid: String?
        let subcategory: String?
        
        init(dictionary: [String: Any]) {
            self.index_url = dictionary["index_url"] as? String? ?? ""
            self.logo_url = dictionary["logo_url"] as? String? ?? ""
            self.offer_category = dictionary["offer_category"] as? String? ?? ""
            self.title = dictionary["title"] as? String? ?? ""
            self.subtitle = dictionary["subtitle"] as? String? ?? ""
            self.session_uuid = dictionary["session_uuid"] as? String? ?? ""
            
    //        let extra_fields = dictionary["extra_fields"] as? [String: Any]? ?? ["":""]
            let extra_fields = dictionary["extra_fields"] as AnyObject
            var extra_fields_parsed:[String: Any]
            
            let json = extra_fields.data(using: String.Encoding.utf8.rawValue)
            do {
                if let jsonArray = try JSONSerialization.jsonObject(with: json!, options: .allowFragments) as? [String:AnyObject]
                {
                    extra_fields_parsed = jsonArray;
                    self.subcategory = extra_fields_parsed["subcategory"] as? String? ?? ""
                } else {
                    self.subcategory = ""
                    print("bad json")
                }
            } catch let error as NSError {
                self.subcategory = ""
                print(error)
            }
            
        }
    }

    public class CampaignDataModel {
        var data: Array<CampaignItemModel> = []

        init() { //initializer method
            let instanceOfMyApi = MyApi()
            let products = instanceOfMyApi.getInbox() as NSMutableArray?
            
            var giftsArray:Array<CampaignItemModel> = []

            
            for gift in products ?? [] {
                let tempGift = CampaignItemModel(dictionary: gift as! [String : Any])
                giftsArray.append(tempGift)
            }
            
            self.data = giftsArray;
            
        }

        var getData: Array<CampaignItemModel> {
            get { // getter
                return data
            }
        }
    }
    
    public func getCampaigns() -> Array<CampaignItemModel> {
        return CampaignDataModel().getData
    }
    
    public func openCoupon(parent: UIView, coupon: CouponSetItemModel) -> UIViewController {
        return UIHostingController(rootView: CouponView(parentView: parent, coupon: coupon))
    }
    
    public func openCouponBarcode(parent: UIView, coupon: CouponItemModel) -> UIViewController {
        return UIHostingController(rootView: CouponBarcodeView(parentView: parent, coupon: coupon))
    }
    
    public func openSteps(parent: UIView) -> UIViewController {
        return UIHostingController(rootView: StepsView(parentView: parent))
    }
}