//
//  KMWatermarkAdjectiveOutsideView.swift
//  PDF Master
//
//  Created by tangchao on 2022/12/16.
//

import Cocoa

class KMWatermarkAdjectiveOutsideView: KMWatermarkAdjectiveBaseView {

    var titleLabel: NSTextField!
    var rotateButton: NSButton!
    var ratateComboBox: NSView!
    var alphaButton: NSButton!
    var alphaComboBox: NSComboBox!
    var pageAboveComboBox: NSComboBox!
    
    var rComboBox: NSComboBox!
    
    var pageTargetScale: NSButton!
    var pageTargetScaleComboBox: NSComboBox!
    
    override var isFlipped: Bool {
        return true
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        self.titleLabel = NSTextField(labelWithString: "")
        self.rotateButton = NSButton()
        self.ratateComboBox = NSView()
        self.alphaButton = NSButton()
        self.alphaComboBox = NSComboBox()
        self.pageAboveComboBox = NSComboBox()
        self.pageTargetScale = NSButton(checkboxWithTitle: NSLocalizedString("Scale relative to target page", comment: ""), target: self, action: #selector(pageTargetScaleAction))
        self.pageTargetScaleComboBox = NSComboBox()
        self.addSubview(self.titleLabel)
        self.addSubview(self.rotateButton)
        self.addSubview(self.ratateComboBox)
        self.addSubview(self.alphaButton)
        self.addSubview(self.alphaComboBox)
        self.addSubview(self.pageAboveComboBox)
        
        self.rComboBox = NSComboBox()
        self.addSubview(self.rComboBox)
        self.addSubview(self.pageTargetScale)
        self.addSubview(self.pageTargetScaleComboBox)
//        
        titleLabel.stringValue = NSLocalizedString("Appearance", comment: "")
        
        rotateButton.isBordered = false
        rotateButton.title = NSLocalizedString("", comment: "")
        rComboBox.isEditable = false
        rComboBox.delegate = self
        rComboBox.removeAllItems()
        for i in 0 ... 360 {
            rComboBox.addItem(withObjectValue: i)
        }
        rComboBox.selectItem(withObjectValue: 0)
        
        alphaButton.isBordered = false
        alphaButton.title = NSLocalizedString("", comment: "")
        alphaComboBox.isEditable = false
        alphaComboBox.delegate = self
        alphaComboBox.removeAllItems()
        for i in 0 ... 100 {
            alphaComboBox.addItem(withObjectValue: "\(i)%")
        }
        alphaComboBox.selectItem(withObjectValue: "100%")
        
        pageAboveComboBox.isEditable = false
        pageAboveComboBox.delegate = self
        pageAboveComboBox.removeAllItems()
        pageAboveComboBox.addItems(withObjectValues: [NSLocalizedString("At the top of the page", comment: ""),NSLocalizedString("At the top of the page", comment: "")])
        pageAboveComboBox.selectItem(at: 0)
        
        self.pageTargetScaleComboBox.delegate = self
        self.pageTargetScaleComboBox.removeAllItems()
        for i in 0 ... 100 {
            pageTargetScaleComboBox.addItem(withObjectValue: "\(i)%")
        }
        self.pageTargetScaleComboBox.selectItem(withObjectValue: "100%")
        self.pageTargetScale.state = .on
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func layout() {
        super.layout()

        let width: CGFloat = NSWidth(self.bounds)
        let heigth: CGFloat = NSHeight(self.bounds)

        let leftMargin: CGFloat = 16
        self.titleLabel.frame = NSMakeRect(leftMargin, 0, width-2*leftMargin, 16)
        let rotateY = self.titleLabel.frame.maxY+10
        self.rotateButton.frame = NSMakeRect(leftMargin, rotateY, 22, 22)
        self.rComboBox.frame = NSMakeRect(self.rotateButton.frame.maxX+10, rotateY, 80, 22)
        self.alphaButton.frame = NSMakeRect(self.rComboBox.frame.maxX+10, rotateY, 22, 22)
        self.alphaComboBox.frame = NSMakeRect(self.alphaButton.frame.maxX+10, rotateY, 80, 22)
        self.pageAboveComboBox.frame = NSMakeRect(leftMargin, self.rotateButton.frame.maxY+10, width-1*leftMargin, 22)
        self.pageTargetScale.frame = NSMakeRect(leftMargin, self.pageAboveComboBox.frame.maxY+10, width-2*leftMargin, 16)
        self.pageTargetScaleComboBox.frame = NSMakeRect(leftMargin, self.pageTargetScale.frame.maxY+10, 100, 22)
    }
    
    
    override func setModel(model: KMWatermarkModel) {
        super.setModel(model: model)

        rComboBox.stringValue = "\(abs(Int(-model.rotation)))"
        alphaComboBox.stringValue = "\(Int(model.opacity*100))%"
        if (model.isFront) {
            pageAboveComboBox.selectItem(at: 0)
        } else {
            pageAboveComboBox.selectItem(at: 1)
        }
    }
    
    @objc func pageTargetScaleAction(sender: NSButton) {
        self.pageTargetScaleComboBox.isEnabled = sender.state == .on
        
        if (sender.state == .on) {
            guard let callback = itemClick else {
                return
            }
            
            callback(4, pageTargetScaleComboBox.indexOfSelectedItem)
        } else {
            guard let callback = itemClick else {
                return
            }
            
            callback(4, 50)
        }
    }
}

extension KMWatermarkAdjectiveOutsideView: NSComboBoxDelegate {
    func comboBoxSelectionDidChange(_ notification: Notification) {
        if (rComboBox.isEqual(to: notification.object)) { /// 旋转
            if (rComboBox.indexOfSelectedItem < 0) {
                return
            }
            
            guard let callback = itemClick else {
                return
            }
            
            callback(1, rComboBox.indexOfSelectedItem)
        } else if (alphaComboBox.isEqual(to: notification.object)) { /// 透明度
            if (alphaComboBox.indexOfSelectedItem < 0) {
                return
            }

            guard let callback = itemClick else {
                return
            }
            
            callback(2, alphaComboBox.indexOfSelectedItem)
        } else if (pageAboveComboBox.isEqual(to: notification.object)) {
            if (pageAboveComboBox.indexOfSelectedItem < 0) {
                return
            }
            
            guard let callback = itemClick else {
                return
            }
            
            callback(3, pageAboveComboBox.indexOfSelectedItem)
        } else if (pageTargetScaleComboBox.isEqual(to: notification.object)) {
            if (pageTargetScaleComboBox.indexOfSelectedItem < 0) {
                return
            }
            
            guard let callback = itemClick else {
                return
            }
            
            callback(4, pageTargetScaleComboBox.indexOfSelectedItem)
        }
    }
}