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

import Foundation
import AppKit

class ImageCompareCellView : NSTableCellView {
    @IBOutlet var _titleLbl : NSTextField!
    @IBOutlet var _degreeInfoLbl : NSTextField!
    
    
    @IBOutlet var _contentView : NSView!
    @IBOutlet var _imageView01 : NSImageView!
    @IBOutlet var _imageView02 : NSImageView!
    
    @IBOutlet var _imageView03 : NSImageView!
    @IBOutlet var _imageView04 : NSImageView!
    
    @IBOutlet var _sepLine : NSView!
    
    @IBOutlet var _segmentedControl : NSSegmentedControl! // 0 - Compare, 1 - Cover Compare
    
    @IBOutlet var _showCoverBtn : NSButton!
    
    var _title : String!
    var _fileInfo : NSMutableDictionary!
    
    class func shared() -> ImageCompareCellView? {
        var objects : NSArray!
        
        Bundle.main.loadNibNamed("ImageCompareCellView", owner: nil, topLevelObjects: &objects)
        
        for tView in objects {
            if let tv = tView as? ImageCompareCellView {
                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 setFileInfo(_ fileInfo:NSMutableDictionary) {
        _fileInfo = fileInfo;
        
        if nil != _fileInfo {
            let comparePath = _fileInfo.comparePath()
            let resultPath = _fileInfo.resultPath()
            
            let image = NSImage.init(contentsOfFile: comparePath)
            _imageView01.image = image;
            
            let image2 = NSImage.init(contentsOfFile: resultPath)
            _imageView02.image = image2;
            
            setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]:\(_fileInfo.fileName())"))

            let degree = _fileInfo.degree()
            if degree == -1 {
                _degreeInfoLbl.stringValue = "文件不存在"
                _degreeInfoLbl.textColor = NSColor.red
            } else {
                if abs(degree - 100) > 0 {
                    _degreeInfoLbl.textColor = NSColor.red
                }else {
                    _degreeInfoLbl.textColor = NSColor.blue
                }
                _degreeInfoLbl.stringValue = NSString(format: "相似度:%.2f%%", degree) as String
            }
        }else {
            setTitle("")
            _degreeInfoLbl.stringValue = ""
        }
    }
    
    public func fileInfo() -> NSMutableDictionary {
        return _fileInfo
    }
    
    // IBAction
    @IBAction func segmentAction(_ sender: NSSegmentedControl) {
        if nil == _fileInfo {
            _imageView01.image = nil;
            _imageView02.image = nil;
            _imageView03.image = nil;
            _imageView04.image = nil;
            
            return
        }
        
        _showCoverBtn.isHidden = sender.indexOfSelectedItem == 0
        if sender.indexOfSelectedItem == 0 {
            _imageView03.image = nil;
            _imageView04.image = nil;
            
            let comparePath = _fileInfo.comparePath()
            let resultPath = _fileInfo.resultPath()
            
            let image = NSImage.init(contentsOfFile: comparePath)
            _imageView01.image = image;
            
            let image2 = NSImage.init(contentsOfFile: resultPath)
            _imageView02.image = image2;
        }else {
            _imageView01.image = nil;
            _imageView02.image = nil;
            _showCoverBtn.state = .on
            
            let comparePath = _fileInfo.comparePath()
            let resultPath = _fileInfo.resultPath()
            
            
            let image = NSImage.init(contentsOfFile: comparePath)
            _imageView03.image = image;
            _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
            
//            let image2 = NSImage.init(contentsOfFile: resultPath)
//            _imageView04.image = image2;
            DispatchQueue.global().async {
                let image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)

                DispatchQueue.main.sync {
                    self._imageView04.image = image2
                }
            }
        }
    }
    
    @IBAction func showCoverAction(_ sender:NSButton) {
        if sender.state == .on {
            _showCoverBtn.title = "隐藏覆盖层"
        }else {
            _showCoverBtn.title = "显示覆盖层"
        }
        
        _imageView04.isHidden = _showCoverBtn.state == .off || _segmentedControl.indexOfSelectedItem != 1
    }
    
}
