//
//  ViewController.swift
//  KMAdvertisementDemo_Mac
//
//  Created by lizhe on 2022/11/23.
//

import Cocoa
import KMAdvertisement

class ViewController: NSViewController {

    //list
    @IBOutlet weak var showListView: NSView!
    @IBOutlet weak var tableView: KMAdvertisementTableView!
    
    //view
    @IBOutlet weak var showView: NSView!
    @IBOutlet weak var advertisementView: KMAdvertisementShowView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.showListView.wantsLayer = true
        self.showListView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.3).cgColor

        // Do any additional setup after loading the view.
        //设置基础参数
        KMAdvertisementManager.manager.initConfig(appName: .PDFReaderProMac,
                                                  subscribeType: .unsubscribed,
                                                  platform: .Mac)
        //测试模式，默认false
        KMAdvertisementManager.manager.debug = true
        
        //获取广告数据
        KMAdvertisementManager.manager.fetchDataWithResponseObject { [unowned self] data, responseObject, error in
            if data != nil {
                for model in data! {
                    if model.showType == .list {
                        //视图加载方法一
                        let view = KMAdvertisementManager.manager.show(type: KMAdvertisementShowType.list, data: model, superView: self.showListView) { actionType, actionItem in
                            print("list 1")
                            if actionItem.actionType == .URL {
                                let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: actionItem.linkURL)
                                if NSWorkspace.shared.open(URL.init(string: string)!) {
                                    NSWorkspace.shared.open(URL.init(string: string)!)
                                }
                            } else {
                                let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: actionItem.linkURL)
                                if NSWorkspace.shared.open(URL.init(string: string)!) {
                                    NSWorkspace.shared.open(URL.init(string: string)!)
                                }
                            }
                        } _: { data in
                            print("数据刷新了")
                        }

                        //视图加载方法二
                        self.tableView.inputData = model
                        self.tableView.actionCompletion = {actionType, actionItem in
                            print("list 2")
                        }
                    }

                    if model.showType == .view {
                        //视图加载方法一
                        let view =  KMAdvertisementManager.manager.show(type: KMAdvertisementShowType.view, data: model, superView: self.showView) { actionType, actionItem in
                            print("view 1")
                        } _: { data in
                            print("数据刷新了")
                        }

                        //视图加载方法二
                        if self.advertisementView != nil {
                            self.advertisementView.inputData = model
                            self.advertisementView.actionCompletion = {actionType, actionItem in
                                print("view 2")
                            }
                        }
                    }
                }
            }
        }
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

