//
//  KMAdvertisementShowScrollCell_iOS.swift
//  KMAdvertisement
//
//  Created by lizhe on 2022/11/29.
//

import UIKit
import Kingfisher

typealias CancelActionBlock = () -> Void
typealias ButtonActionBlock = (_ item: KMAdvertisementModelItem) -> Void

class KMAdvertisementShowScrollCell_iOS: UICollectionViewCell {

    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var actionButton: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    
    var model: KMAdvertisementModelItem! {
        didSet {
            self.reloadData()
        }
    }
    
    var cancelActionBlock: CancelActionBlock?
    var buttonActionBlock: ButtonActionBlock?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
//        self.backgroundColor = UIColor.init(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1)
        
//        let bundle = Bundle(for: self.classForCoder.self)
//        let path = bundle.path(forResource: "KMAdvertisement", ofType: "bundle") ?? ""
//        let sdkBundle = Bundle(path: path)
//        let filePath = sdkBundle?.path(forResource: "dark_brower_delect", ofType: "pdf")
//        let image = UIImage.init(contentsOfFile: filePath!)
//        self.cancelButton.setTitle("", for: UIControl.State.normal)
//        self.cancelButton.setImage(image, for: UIControl.State.normal)
//        self.cancelButton.isHidden = true
        
        self.actionButton.setTitle("", for: UIControl.State.normal)
    }
    
    func reloadData() {
        if model?.title?.language != nil {
            self.titleLabel.text = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.title?.language)
        }
        
        self.titleLabel.font = UIFont.init(name: model?.title?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.title?.font?.size ?? 13))
        
        self.actionButton.clipsToBounds = true
        self.actionButton.layer.cornerRadius = model.button?.background?.layer?.cornerRadius ?? 15
        self.actionButton.layer.borderWidth = model.button?.background?.layer?.borderWidth ?? 0
        self.actionButton.titleLabel?.font = UIFont.init(name: model?.button?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.button?.font?.size ?? 13))
        
        if model?.button?.language != nil {
            self.actionButton.setTitle(KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.button?.language), for: UIControl.State.normal)
        }
        
        self.imageView.clipsToBounds = true
        self.imageView.layer.cornerRadius = model.imageURL?.background?.layer?.cornerRadius ?? 8
        self.imageView.layer.borderWidth = model.imageURL?.background?.layer?.borderWidth ?? 0
        
        self.updateColor(highlight: false)
    }
    
    func updateColor(highlight: Bool) {
        if model.title?.color != nil {
            self.titleLabel.textColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.color)!, highlight: highlight)
        }
        
        if model.button?.background?.color != nil {
            self.actionButton.layer.borderColor = KMAdvertisementModelTransition.transitionColor(color: (model.button?.background?.color)!, highlight: highlight).cgColor
        }
        
        if model.button?.color != nil {
            self.actionButton.tintColor = KMAdvertisementModelTransition.transitionColor(color: (model.button?.color)!, highlight: highlight)
        }
        
        if model.imageURL != nil {
            self.imageView?.kf.setImage(with: URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.imageURL!, highlight: highlight)))
        }
    }
    
    
    @IBAction func cancelAction(_ sender: Any) {
        if self.cancelActionBlock != nil {
            self.cancelActionBlock!()
        }
    }
    
    @IBAction func buttonAction(_ sender: Any) {
        if self.buttonActionBlock != nil {
            self.buttonActionBlock!(self.model)
        }
    }
    
}
