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

import Foundation
import AppKit

public protocol TestCaseCellViewDelegate : NSObjectProtocol {
    func selectKeyDidUpdate(_ cell:NSTableCellView?)
}

class TestCaseCellView : NSTableCellView {
    @IBOutlet var _titleLbl : NSTextField!
    @IBOutlet var _keyContentView : NSView!
    @IBOutlet var _sepLine : NSView!
    @IBOutlet var _activityView : ActivityView!
    
    @IBOutlet var _toggleBtn : NSButton!
    
    var _keyViews : NSMutableArray!
    var _autoTestObj : AutoTest?
    
    var _title : String!
    var _checkKeys : NSArray!
    
    var delegate : TestCaseCellViewDelegate?
    
    class func shared() -> TestCaseCellView? {
        var objects : NSArray!
        
        Bundle.main.loadNibNamed("TestCaseCellView", owner: nil, topLevelObjects: &objects)
        
        for tView in objects {
            if let tv = tView as? TestCaseCellView {
                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
    }
    
    let kTFDefaultHeight = 22.0
    public func setCheckKeys(_ keys:NSArray) {
        
        _checkKeys = keys;
        
        if _keyViews == nil {
            _keyViews = NSMutableArray()
        }
        while (_keyViews.count != _checkKeys.count) {
            if (_keyViews.count > _checkKeys.count) {
                (_keyViews.lastObject as! NSTextField).removeFromSuperview()
                _keyViews.removeLastObject()
            }else {
                let tf = NSTextField.init(frame: CGRectMake(0, 4, 30, kTFDefaultHeight));
                _keyContentView.addSubview(tf);
                tf.backgroundColor = .clear
                _keyViews.add(tf)
            }
        }
        
        var width = 0.0;
        
        for tf in _keyViews {
            let i = _keyViews.index(of: tf)
            let tfView = tf as! NSTextField
            
            tfView.stringValue = _checkKeys[i] as! String;
            
            tfView.sizeToFit()
            
            tfView.setFrameOrigin(NSPoint.init(x: width, y: 0))
            tfView.setFrameSize(NSSize.init(width: (tfView.frame.size.width.advanced(by: 2.0)), height: kTFDefaultHeight));
            tfView.autoresizingMask = .maxXMargin.union(.maxYMargin)
            tfView.wantsLayer = true
            tfView.layer?.cornerRadius = 8
            tfView.layer?.backgroundColor = NSColor.clear.cgColor
            tfView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.6).cgColor
            tfView.layer?.borderWidth = 1
            tfView.isEditable = false
            
            width = width + tfView.frame.size.width + 3
        }
//        _keyContentView.frame = NSRect(x: 0, y: 0, width: width, height: kTFDefaultHeight)
//        _keyContentView.setFrameSize(NSSize.init(width: width, height: kTFDefaultHeight))
        
        // update Toggle Btn
        if _checkKeys.count == 0 {
            _toggleBtn.image = NSImage(named: "Toggle_off.png")
        }else if _checkKeys.count != _autoTestObj?.keys().count {
            _toggleBtn.image = NSImage(named: "Toggle_half.png")
        }else {
            _toggleBtn.image = NSImage(named: "Toggle_on.png")
        }
    }
    
    public func checkKeys() -> NSArray? {
        return _checkKeys;
    }
    
    
    public func autoTestObj() -> AutoTest? {
        return _autoTestObj
    }
    
    public func setAutoTestObj(_ obj: AutoTest?) {
        _autoTestObj = obj
        
//        self.setAccessibilityEnabled((_autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true))
        if nil != _autoTestObj {
            self.setTitle((_autoTestObj?.name())!)
            self.setCheckKeys((_autoTestObj?.selectedKeys())!)

//            self.wantsLayer = true
//            if _autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true {
//                self.layer?.opacity = 1
//            }else {
//                self.layer?.opacity = 0.5
//            }
        }else {
            self.setTitle("")
            self.setCheckKeys([])
            
//            self.wantsLayer = true
//            self.layer?.opacity = 0.5
        }
        
        updateStatus()
    }
    
    public func updateStatus() {
        _activityView.setDegree(_autoTestObj?.degree() ?? 0)
        _activityView.setActivityStatus(ActivityStatus(rawValue: (_autoTestObj?.status())!.rawValue) ?? .Normal)
    }
    
    // IBAction
    @IBAction func toggleAction(_ sender:NSButton) {
        if _autoTestObj != nil {
            if _checkKeys.count == 0 {
                _autoTestObj?.setSelectedKeys((_autoTestObj?.keys())!)
            }else if _checkKeys.count != _autoTestObj?.keys().count {
                _autoTestObj?.setSelectedKeys((_autoTestObj?.keys())!)
            }else {
                _autoTestObj?.setSelectedKeys([])
            }
            self.setCheckKeys((_autoTestObj?.selectedKeys())!)
            
            if delegate != nil {
                delegate?.selectKeyDidUpdate(self)
            }
        }
    }
    
}
