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

import Cocoa

open class KMAdvertisementShowView: KMAdvertisementBaseView {

    @IBOutlet var contentView: NSView!
    
    @IBOutlet weak var cancelButton: NSButton!
    
    @IBOutlet weak var imageView: NSImageView!
    
    lazy var presenter: KMAdvertisementTableViewPresenter! = KMAdvertisementTableViewPresenter()
    
    //内部使用数据
    fileprivate var data: [KMAdvertisementModel.Section]?
    /**
      @abstract 外部传入数据
      @param inputData 文件路劲
      */
    open var inputData: KMAdvertisementModel! {
        didSet {
            self.presenter.initPresenter(view: self, data: inputData)
        }
    }
    
    convenience init (data: KMAdvertisementModel, superView: NSView) {
        self.init(frame: superView.bounds)
        self.presenter.initPresenter(view: self, data: data)
        superView.addSubview(self)
        self.autoresizingMask = [.height , .width]
    }
    
    // MARK: 初始化
    public required init?(coder decoder: NSCoder) {
        super.init(coder: decoder)
        initContentView()
        setup()
    }
    
    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initContentView()
        setup()
    }
    

    private func initContentView() {
        //绑定xib
        let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
                             bundle: Bundle(for: self.classForCoder.self))!
        resource.instantiate(withOwner: self, topLevelObjects: nil)
        addSubview(contentView)
        contentView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            contentView.topAnchor.constraint(equalTo: topAnchor),
            contentView.leftAnchor.constraint(equalTo: leftAnchor),
            contentView.rightAnchor.constraint(equalTo: rightAnchor),
            contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
        contentView.updateConstraintsForSubtreeIfNeeded()
    }
    
    func setup() {
        let bundle = Bundle(for: self.classForCoder.self)
        let path = bundle.path(forResource: "KMAdvertisement", ofType: "bundle") ?? ""
        let sdkBundle = Bundle(path: path)
        let filePath = sdkBundle?.pathForImageResource("ad_cancel_button00")
        let image = NSImage.init(contentsOfFile: filePath!)
        self.cancelButton.image = image
    }
    
    public override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Drawing code here.
    }
    
    func reloadData() {
     
        let url = URL(string: self.data?.first?.content.first?.imageURL?.normal?.en ?? "")
        self.imageView.kf.setImage(with: url)
    }
    
    @IBAction func cancelAction(_ sender: Any) {
        self.removeFromSuperview()
    }
    
    open override func mouseDown(with event: NSEvent) {
        let content = self.data?.first?.content.first
        if actionCompletion != nil {
            actionCompletion!(content!)
        }
    }
}

extension KMAdvertisementShowView: KMAdvertisementTableViewPresenterDelegate {
    func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModel.Section>) {
        self.data = data
        self.reloadData()
    }
}
