//
//  KMNPopDetailsViewController.swift
//  PDF Reader Pro
//
//  Created by 丁林圭 on 2024/12/6.
//

import Cocoa

import KMComponentLibrary

typealias FontChangeAlighCallback = (_ alight:NSTextAlignment) -> ()

typealias ObjectChangeAlighCallback = (_ alight:ObjectAlignType) -> ()

class KMNPopDetailsViewController: KMNBaseViewController {

    public var detailPopType:DetailPopType = .detailNone {
        didSet {
            switch detailPopType {
            case .freeTextAlight :
                alightRightConstraint.constant = 4
                alightJustifiedButton.isHidden = true
                fontAlightView.layoutSubtreeIfNeeded()
                
                operationWidthConstraint.constant = fontAlightView.bounds.width
                operationHeightConstraint.constant = fontAlightView.bounds.height
                self.view.layoutSubtreeIfNeeded()
                contentBox.contentView = fontAlightView
                break
            case .contentEditAlight:
                alightRightConstraint.constant = 44
                alightJustifiedButton.isHidden = false
                fontAlightView.layoutSubtreeIfNeeded()
                
                operationWidthConstraint.constant = fontAlightView.bounds.width
                operationHeightConstraint.constant = fontAlightView.bounds.height
                self.view.layoutSubtreeIfNeeded()
                contentBox.contentView = fontAlightView
                break
            case .doubleObjectAlight:
                objectRightConstraint.constant = 4
                objectAlightView.layoutSubtreeIfNeeded()
                objectAverageHorButton.isHidden = true
                objectAverageVerButton.isHidden = true

                operationWidthConstraint.constant = objectAlightView.bounds.width
                operationHeightConstraint.constant = objectAlightView.bounds.height
                self.view.layoutSubtreeIfNeeded()
                contentBox.contentView = objectAlightView
                
                break
            case .freeTextDoubleAlight:
                objectRightConstraint.constant = 4
                objectAlightView.layoutSubtreeIfNeeded()
                objectAverageHorButton.isHidden = true
                objectAverageVerButton.isHidden = true

                operationWidthConstraint.constant = objectAlightView.bounds.width
                operationHeightConstraint.constant = objectAlightView.bounds.height
                self.view.layoutSubtreeIfNeeded()
                contentBox.contentView = objectAlightView
                
                break
            case .multpleObjectAlight:
                objectRightConstraint.constant = 80
                objectAlightView.layoutSubtreeIfNeeded()
                objectAverageHorButton.isHidden = false
                objectAverageVerButton.isHidden = false
                
                operationWidthConstraint.constant = objectAlightView.bounds.width
                operationHeightConstraint.constant = objectAlightView.bounds.height
                self.view.layoutSubtreeIfNeeded()
                contentBox.contentView = objectAlightView
                break
            case .freeTextMultpleAlight:
                objectRightConstraint.constant = 80
                objectAlightView.layoutSubtreeIfNeeded()
                objectAverageHorButton.isHidden = false
                objectAverageVerButton.isHidden = false

                operationWidthConstraint.constant = objectAlightView.bounds.width
                operationHeightConstraint.constant = objectAlightView.bounds.height
                self.view.layoutSubtreeIfNeeded()
                contentBox.contentView = objectAlightView
                break
            case .detailNone:
                break
            }
        }
    }
    
    public var fontAlight:NSTextAlignment = .left {
        didSet {
            if alightRightButton.properties.state == .pressed {
                alightRightButton.properties.state = .normal
                alightRightButton.reloadData()
            }
            if alightCenteButton.properties.state == .pressed {
                alightCenteButton.properties.state = .normal
                alightCenteButton.reloadData()
            }
            if alightJustifiedButton.properties.state == .pressed {
                alightJustifiedButton.properties.state = .normal
                alightJustifiedButton.reloadData()
            }
            if alightLeftButton.properties.state == .pressed {
                alightLeftButton.properties.state = .normal
                alightLeftButton.reloadData()
            }
            
            if fontAlight == .left {
                alightLeftButton.properties.state = .pressed
                alightLeftButton.reloadData()
            } else if fontAlight == .right {
                alightRightButton.properties.state = .pressed
                alightRightButton.reloadData()
            } else if fontAlight == .center {
                alightCenteButton.properties.state = .pressed
                alightCenteButton.reloadData()
            } else if fontAlight == .justified {
                alightJustifiedButton.properties.state = .pressed
                alightJustifiedButton.reloadData()
            }
        }
    }
    
    public var objectAlignType:ObjectAlignType = .alightLeft {
        didSet {
            if objectAlightLeftButton.properties.state == .pressed {
                objectAlightLeftButton.properties.state = .normal
                objectAlightLeftButton.reloadData()
            }
            if objectAlightRightButton.properties.state == .pressed {
                objectAlightRightButton.properties.state = .normal
                objectAlightRightButton.reloadData()
            }
            if objectAlightTopButton.properties.state == .pressed {
                objectAlightTopButton.properties.state = .normal
                objectAlightTopButton.reloadData()
            }
            if objectAlightHorButton.properties.state == .pressed {
                objectAlightHorButton.properties.state = .normal
                objectAlightHorButton.reloadData()
            }
            if objectAlightBottomButton.properties.state == .pressed {
                objectAlightBottomButton.properties.state = .normal
                objectAlightBottomButton.reloadData()
            }
            if objectAverageVerButton.properties.state == .pressed {
                objectAverageVerButton.properties.state = .normal
                objectAverageVerButton.reloadData()
            }
            if objectAverageHorButton.properties.state == .pressed {
                objectAverageHorButton.properties.state = .normal
                objectAverageHorButton.reloadData()
            }
            if objectAlightVerButton.properties.state == .pressed {
                objectAlightVerButton.properties.state = .normal
                objectAlightVerButton.reloadData()
            }
            
            var button:ComponentButton = objectAlightLeftButton
            switch objectAlignType {
            case .alightLeft :
                button = objectAlightLeftButton
                break
            case .alightRight:
                button = objectAlightRightButton
                break
            case .alightTop:
                button = objectAlightTopButton
                break
            case .alightVer:
                button = objectAlightVerButton
                break
            case .alightHor:
                button = objectAlightHorButton
                break
            case .alightBottom:
                button = objectAlightBottomButton
                break
            case .averageVer:
                button = objectAverageVerButton
                break
            case .averageHor:
                button = objectAverageHorButton
                break
            }
            button.properties.state = .pressed
            button.reloadData()
        }
    }
    
    var fontChangeAlighCallback: FontChangeAlighCallback?
    var objectChangeAlighCallback: ObjectChangeAlighCallback?

    @IBOutlet weak var contentBox: NSBox!
    @IBOutlet weak var operationWidthConstraint: NSLayoutConstraint!
    @IBOutlet weak var operationHeightConstraint: NSLayoutConstraint!

    @IBOutlet var fontAlightView: NSView!
    @IBOutlet var alightLeftButton: ComponentButton!
    @IBOutlet var alightRightButton: ComponentButton!
    @IBOutlet var alightCenteButton: ComponentButton!
    @IBOutlet var alightJustifiedButton: ComponentButton!
    @IBOutlet weak var alightRightConstraint: NSLayoutConstraint!
    
    @IBOutlet var objectAlightView: NSView!
    @IBOutlet var objectAlightLeftButton: ComponentButton!
    @IBOutlet var objectAlightRightButton: ComponentButton!
    @IBOutlet var objectAlightTopButton: ComponentButton!
    @IBOutlet var objectAlightVerButton: ComponentButton!
    @IBOutlet var objectAlightHorButton: ComponentButton!
    @IBOutlet var objectAlightBottomButton: ComponentButton!
    @IBOutlet var objectAverageVerButton: ComponentButton!
    @IBOutlet var objectAverageHorButton: ComponentButton!
    @IBOutlet weak var objectRightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func initContentView() {
        super.initContentView()
        
        alightLeftButton.properties = ComponentButtonProperty(type: .text_gray,
                                                              size: .s,
                                                              state: .normal,
                                                              onlyIcon: true,
                                                              icon:NSImage(named: "KMNImageNameListViewPopAlightLeft"))
        alightLeftButton.setTarget(self, action: #selector(alightLeftButtonClicked(_ :)))
        
        alightRightButton.properties = ComponentButtonProperty(type: .text_gray,
                                                               size: .s,
                                                               state: .normal,
                                                               onlyIcon: true,
                                                               icon:NSImage(named: "KMNImageNameListViewPopAlightRight"))
        alightRightButton.setTarget(self, action: #selector(alightRightButtonClicked(_ :)))
        
        alightCenteButton.properties = ComponentButtonProperty(type: .text_gray,
                                                               size: .s,
                                                               state: .normal,
                                                               onlyIcon: true,
                                                               icon:NSImage(named: "KMNImageNameListViewPopAlightCenter"))
        alightCenteButton.setTarget(self, action: #selector(alightCenterButtonClicked(_ :)))
        
        alightJustifiedButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                   size: .s,
                                                                   state: .normal,
                                                                   onlyIcon: true,
                                                                   icon:NSImage(named: "KMNImageNameListViewPopAlightJus"))
        alightJustifiedButton.setTarget(self, action: #selector(alightJustButtonClicked(_ :)))
        
        objectAlightLeftButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                    size: .s,
                                                                    state: .normal,
                                                                    onlyIcon: true,
                                                                    icon:NSImage(named: "KMNImageNameObjectPopAlightLeft"))
        objectAlightLeftButton.setTarget(self, action: #selector(objectAlightLeftClick(_ :)))
        
        objectAlightRightButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                     size: .s,
                                                                     state: .normal,
                                                                     onlyIcon: true,
                                                                     icon:NSImage(named: "KMNImageNameObjectPopAlightRight"))
        objectAlightRightButton.setTarget(self, action: #selector(objectAlightRightClick(_ :)))
        
        objectAlightTopButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                   size: .s,
                                                                   state: .normal,
                                                                   onlyIcon: true,
                                                                   icon:NSImage(named: "KMNImageNameObjectPopAlightTop"))
        objectAlightTopButton.setTarget(self, action: #selector(objectAlightTopClick(_ :)))
        
        objectAlightVerButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                   size: .s,
                                                                   state: .normal,
                                                                   onlyIcon: true,
                                                                   icon:NSImage(named: "KMNImageNameObjectPopAlightVer"))
        objectAlightVerButton.setTarget(self, action: #selector(objectAlightVerClick(_ :)))
        
        objectAlightHorButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                   size: .s,
                                                                   state: .normal,
                                                                   onlyIcon: true,
                                                                   icon:NSImage(named: "KMNImageNameObjectPopAlightHor"))
        objectAlightHorButton.setTarget(self, action: #selector(objectAlightHorClick(_ :)))
        
        objectAlightBottomButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                      size: .s,
                                                                      state: .normal,
                                                                      onlyIcon: true,
                                                                      icon:NSImage(named: "KMNImageNameObjectPopAlightBottom"))
        objectAlightBottomButton.setTarget(self, action: #selector(objectAlightBottomClicked(_ :)))
        
        objectAverageVerButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                    size: .s,
                                                                    state: .normal,
                                                                    onlyIcon: true,
                                                                    icon:NSImage(named: "KMNImageNameObjecAlignAverageVer"))
        objectAverageVerButton.setTarget(self, action: #selector(objectAverageVerClicked(_ :)))
        
        objectAverageHorButton.properties = ComponentButtonProperty(type: .text_gray,
                                                                    size: .s,
                                                                    state: .normal,
                                                                    onlyIcon: true,
                                                                    icon:NSImage(named: "KMNImageNameObjecAlignAverageHor"))
        objectAverageHorButton.setTarget(self, action: #selector(objectAverageHorClicked(_ :)))
        
    }
    
    override func updateUIThemeColor() {
        super.updateUIThemeColor()
        
        contentBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/popup")
        contentBox.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/3-default")
    }
    
    override func updateUILanguage() {
        super.updateUILanguage()
        
    }
    
    //MARK: - Action
    @objc func alightLeftButtonClicked(_ sender: NSView) {
        if fontAlight != .left {
            fontAlight = .left
            fontChangeAlighCallback?(fontAlight)
        }
    }
    
    @objc func alightRightButtonClicked(_ sender: NSView) {
        if fontAlight != .right {
            fontAlight = .right
            fontChangeAlighCallback?(fontAlight)
        }
    }
    
    @objc func alightCenterButtonClicked(_ sender: NSView) {
        if fontAlight != .center {
            fontAlight = .center
            fontChangeAlighCallback?(fontAlight)
        }
    }
    
    @objc func alightJustButtonClicked(_ sender: NSView) {
        if fontAlight != .justified {
            fontAlight = .justified
            fontChangeAlighCallback?(fontAlight)
        }
    }
    
    @objc func objectAlightLeftClick(_ sender: NSView) {
        if objectAlignType != .alightLeft {
            objectAlignType = .alightLeft
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAlightRightClick(_ sender: NSView) {
        if objectAlignType != .alightRight {
            objectAlignType = .alightRight
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAlightTopClick(_ sender: NSView) {
        if objectAlignType != .alightTop {
            objectAlignType = .alightTop
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAlightVerClick(_ sender: NSView) {
        if objectAlignType != .alightVer {
            objectAlignType = .alightVer
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAlightHorClick(_ sender: NSView) {
        if objectAlignType != .alightHor {
            objectAlignType = .alightHor
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAlightBottomClicked(_ sender: NSView) {
        if objectAlignType != .alightBottom {
            objectAlignType = .alightBottom
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAverageVerClicked(_ sender: NSView) {
        if objectAlignType != .averageVer {
            objectAlignType = .averageVer
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
    @objc func objectAverageHorClicked(_ sender: NSView) {
        if objectAlignType != .averageHor {
            objectAlignType = .averageHor
            objectChangeAlighCallback?(objectAlignType)
        }
    }
    
}