//
//  KMPageEditInsertDirectionItemView.swift
//  PDF Master
//
//  Created by tangchao on 2023/1/11.
//

import Cocoa

class KMPageEditInsertDirectionItemView: KMPageEditBaseItemView {
    private var horVC: KMDesignButton?
    private var verVC: KMDesignButton?
    
    override func initSubviews() {
        super.initSubviews()
        
        self.horVC = KMDesignButton(withType: .RadioButton)
        self.addSubview(self.horVC!.view)
        self.horVC?.target = self
        self.horVC?.action = #selector(horAction)
        self.horVC?.stringValue = NSLocalizedString("Horizontal page", comment: "")
        self.horVC?.checkbox_radio()
        
        self.verVC = KMDesignButton(withType: .RadioButton)
        self.addSubview(self.verVC!.view)
        self.verVC?.target = self
        self.verVC?.action = #selector(verAction)
        self.verVC?.stringValue = NSLocalizedString("Vertical pages", comment: "")
        self.verVC?.checkbox_radio()
        
        self.titleLabel.stringValue = NSLocalizedString("Direction", comment: "")
        self.titleLabel.font = .SFProTextSemiboldFont(12)
        self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
        
        self.horVC?.state = .Norm
        self.verVC?.state = .Checked
    }
    
    override func layout() {
        super.layout()
        
        let raidoY: CGFloat = self.titleLabel.frame.maxY
        let radioSize = NSSize(width: 150, height: 22)
        self.verVC?.view.frame = NSMakeRect(self.contentInset.left, raidoY, radioSize.width, radioSize.height)
        self.horVC?.view.frame = NSMakeRect(self.contentInset.left + radioSize.width, raidoY , radioSize.width, radioSize.height)
    }
    
    @objc private func horAction() {
        self.verVC?.state = .Norm
        self.horVC?.state = .Checked
        
        guard let callback = self.itemClick else {
            return
        }
        
        callback(1, "")
    }
    
    @objc private func verAction() {
        self.horVC?.state = .Norm
        self.verVC?.state = .Checked
        
        guard let callback = self.itemClick else {
            return
        }
        
        callback(2, "")
    }
    
    public func switchDirection(isHor: Bool) {
        self.horVC?.state = .Norm
        self.verVC?.state = .Norm
        
        if (isHor) {
            self.horVC?.state = .Checked
        } else {
            self.verVC?.state = .Checked
        }
    }
    
    public func getDirection() -> Int {
        if let state = self.horVC?.state {
            return state == .Checked ? 1 : 0
        }
        return 0
    }
}