//
//  KMAdvertisementTablePresenter.swift
//  KMAdvertisement
//
//  Created by lizhe on 2022/11/28.
//

class KMAdvertisementTableViewPresenter: NSObject {
    lazy var modelData: [KMAdvertisementModelSection] = []
    var view: AnyObject?
    fileprivate weak var delegate: KMAdvertisementTableViewPresenterDelegate?
}

extension KMAdvertisementTableViewPresenter: KMAdvertisementTableViewPresenterProtocol {
    /**
     初始化presenter
     */
    func initPresenter(view:AnyObject, data: KMAdvertisementModel) {
        self.delegate = view as? any KMAdvertisementTableViewPresenterDelegate
        
        self.modelData.removeAll()
        self.modelData = self.fetchData(data: data.content ?? [])
        self.delegate?.showData(presenter: self, data: self.modelData)
    }
    
    func fetchData(data: [KMAdvertisementModelSection]) -> [KMAdvertisementModelSection] {
        if data.count != 0 {
            //获取缓存数据
            if (UserDefaults.standard.object(forKey: "KMAdvertisementShowScroll_iOS") == nil) {
                UserDefaults.standard.set([], forKey: "KMAdvertisementShowScroll_iOS")
            }
            let cacheArray: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowScroll_iOS") as! [String]
            
            var sectionArray: [KMAdvertisementModelSection] = []
            for section in data {
                var itemArray: [KMAdvertisementModelItem] = []
                for item in section.content! {
                    if !cacheArray.contains(item.productID ?? "") {
                        itemArray.append(item)
                    }
                }
                section.content = itemArray
                sectionArray.append(section)
            }
            return sectionArray
        }
        return []
    }
    
    func deleteItem(item: KMAdvertisementModelItem) {
        //添加数据
        if (UserDefaults.standard.object(forKey: "KMAdvertisementShowScroll_iOS") == nil) {
            UserDefaults.standard.set([], forKey: "KMAdvertisementShowScroll_iOS")
        }
        var array: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowScroll_iOS") as! [String]
        
        array.append(item.productID!)
        UserDefaults.standard.set(array, forKey: "KMAdvertisementShowScroll_iOS")
        UserDefaults.standard.synchronize()
        
        self.modelData = self.fetchData(data: self.modelData)
        
        if self.delegate != nil {
            self.delegate?.showData(presenter: self, data: self.modelData)
        }
    }
    
    func cleanAll() {
        self.modelData.removeAll()
        self.delegate?.showData(presenter: self, data: self.modelData)
    }
    
    func free() {
        view = nil
        delegate = nil
    }
}


protocol KMAdvertisementTableViewPresenterDelegate: AnyObject {
    /**
     @abstract 回调，返回处理后的数据<KMBatchProcessingViewModel>类型
     @param presenter
     @param data 返回数据
     */
    func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModelSection>)
}

protocol KMAdvertisementTableViewPresenterProtocol {
    /**
     @abstract 绑定视图与数据
     @param view 视图
     @param data 资源 <KMBatchProcessingViewModel>类型
     */
    func initPresenter(view:AnyObject, data: KMAdvertisementModel)
    /**
     @abstract 删除item
     */
    func deleteItem(item: KMAdvertisementModelItem)
    /**
     @abstract 解除绑定
     */
    func free()
}
