//
//  TeseCaseCell.swift
//  KdanAuto
//
//  Created by 朱东勇 on 2022/11/21.
//

import Foundation
import AppKit

protocol TestFileCellViewDelegate : NSObjectProtocol {
    func fileCellNeedReplace(_ cell:TestFileCellView, fileName:String)
    
    func fileCellNeedShowReport(_ cell:TestFileCellView, fileName:String)
}

class TestFileCellView : NSTableCellView {
    @IBOutlet var _titleLbl : NSTextField!
    @IBOutlet var _sepLine : NSView!
    
    @IBOutlet var _replaceBtn : NSButton!
    @IBOutlet var _testReportBtn : NSButton!
    
    var _title : String!
    var _isExpad: Bool!
    var _typeInfo : NSDictionary!
    
    var _degree : Double = 0
    
    weak public var _delegate : (TestFileCellViewDelegate)?
    
    class func shared() -> TestFileCellView? {
        var objects : NSArray!
        
        Bundle.main.loadNibNamed("TestFileCellView", owner: nil, topLevelObjects: &objects)
        
        for tView in objects {
            if let tv = tView as? TestFileCellView {
                return tv
            }
        }
        
        return nil
    }
    
    override func awakeFromNib() {
        _sepLine.wantsLayer = true;
        _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor
    }
    
    // Setter & Getter
    public func setTitle(_ title:String) {
        _title = title;
        
        _titleLbl.stringValue = _title ?? ""
    }
    
    public func title() -> String? {
        return _title
    }
    
    public func fileType() -> String? {
        return _title
    }
    
    public func setNeedReplaceBtn(_ needReplace:Bool) {
        _replaceBtn.isHidden = !needReplace
    }
    
    public func setTypeInfo(_ typeInfo:NSDictionary) {
        _typeInfo = typeInfo
        
        self.setTitle(_typeInfo["Type"] as! String)
    }
    
    public func typeInfo() -> NSDictionary? {
        return _typeInfo
    }
    
    public func setNeedDegreeBtn(_ needDegree:Bool) {
        _testReportBtn.isHidden = !needDegree
    }
    
    public func setDegree(_ degree:Double) {
        _degree = degree
        
        if fabs(_degree - 100) > 0.01 {
            _testReportBtn.contentTintColor = NSColor.red
        }else {
            _testReportBtn.contentTintColor = NSColor.gray
        }
        
        _testReportBtn.title = NSString(format: "%.0f%%", _degree) as String
    }
    
    // IBActionn
    @IBAction func replaceAction(_ sender:NSButton) {
        if _delegate != nil {
            _delegate?.fileCellNeedReplace(self, fileName: _title)
        }
    }
    
    @IBAction func showReportAction(_ sender:NSButton) {
        if _delegate != nil {
            _delegate?.fileCellNeedShowReport(self, fileName: _title)
        }
    }
}
