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

import Foundation

public class swiftApi {

    public init() {
        
    }
    
    public func getUserTag() -> String {
        return "1"
    }
    
    public func setDFY(couponCode: String, merchantId: String) {
        
    }

    public func setActiveDFYCoupons(campaignIds: Array<String>) {
        
    }
    
    public func setCCMSLoyaltyCampaigns(campaigns: Array<LoyaltyContextualOfferModel>) {
        
    }
    
    public class LoyaltyContextualOfferModel {
        var sessionId: String
        var eligibleAssets: Array<String>
        var id: String
        
        init(sessionId: String, eligibleAssets: Array<String>, id: String) {
            self.sessionId = sessionId
            self.eligibleAssets = eligibleAssets
            self.id = id
        }
    }
    
    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?
        
        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
            
            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 CouponsDataModel {
        var data: Array<CouponItemModel> = []

        init() { //initializer method
            let instanceOfMyApi = MyApi()
            let coupons = instanceOfMyApi.getCoupons()
            var couponsArray:Array<CouponItemModel> = []
            
            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
            }
        }
    }
    
    public func getCoupons() -> Array<CouponItemModel> {
        return CouponsDataModel().getData
    }
    
    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
    }
}