//
//  KMTextAlignmentController.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2024/6/26.
//

import Cocoa

class KMTextAlignmentController: NSViewController {
    @IBOutlet weak var leftBox: NSBox!
    @IBOutlet weak var centerBox: NSBox!
    @IBOutlet weak var rightBox: NSBox!
    
    private var leftVc_: KMDesignButton?
    private var centerVc_: KMDesignButton?
    private var rightVc_: KMDesignButton?
    
    var itemAction: KMCommonClickBlock?
    
    var align: NSTextAlignment = .center {
        didSet {
            let data = self.align
            if data == .left {
                self.leftVc_?.state = .Act
                self.centerVc_?.state = .Norm
                self.rightVc_?.state = .Norm
            } else if data == .center {
                self.leftVc_?.state = .Norm
                self.centerVc_?.state = .Act
                self.rightVc_?.state = .Norm
            } else if data == .right {
                self.leftVc_?.state = .Norm
                self.centerVc_?.state = .Norm
                self.rightVc_?.state = .Act
            }
        }
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        self.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.wantsLayer = true
        
        self.leftVc_ = KMDesignButton(withType: .Image)
        self.leftBox.contentView = self.leftVc_!.view
        self.leftBox.borderWidth = 0
        self.leftVc_?.pagination()
        self.leftVc_?.target = self
        self.leftVc_?.action = #selector(_itemClick)
        self.leftVc_?.image = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")!
        self.leftVc_?.tag = 0
        
        self.leftVc_?.initDefaultValue()
        self.leftVc_?.borderWidth = 0
        self.leftVc_?.borderWidth_hov = 0
        self.leftVc_?.borderWidth_act = 0
        
        self.centerVc_ = KMDesignButton(withType: .Image)
        self.centerBox.contentView = self.centerVc_!.view
        self.centerBox.borderWidth = 0
        self.centerVc_?.pagination()
        self.centerVc_?.target = self
        self.centerVc_?.action = #selector(_itemClick)
        self.centerVc_?.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")!
        self.centerVc_?.tag = 1
        
        self.centerVc_?.initDefaultValue()
        self.centerVc_?.borderWidth = 0
        self.centerVc_?.borderWidth_hov = 0
        self.centerVc_?.borderWidth_act = 0
        
        self.rightVc_ = KMDesignButton(withType: .Image)
        self.rightBox.contentView = self.rightVc_!.view
        self.rightBox.borderWidth = 0
        self.rightVc_?.pagination()
        self.rightVc_?.target = self
        self.rightVc_?.action = #selector(_itemClick)
        self.rightVc_?.image = NSImage(named: "KMImageNameEditPDFAlignRightSelect")!
        self.rightVc_?.tag = 2
        
        self.rightVc_?.initDefaultValue()
        self.rightVc_?.borderWidth = 0
        self.rightVc_?.borderWidth_hov = 0
        self.rightVc_?.borderWidth_act = 0
        
        self.reloadData()
    }
    
    @objc private func _itemClick(_ sender: NSButton) {
        self.itemAction?(sender.tag)
    }
    
    func reloadData() {
        self.view.wantsLayer = true
        self.leftBox.fillColor = .clear
        self.centerBox.fillColor = .clear
        self.rightBox.fillColor = .clear
        if KMAppearance.isDarkMode() {
//            self.view.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
            self.view.layer?.backgroundColor = NSColor(hex: "#26282B").cgColor
            self.leftVc_?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
            self.centerVc_?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
            self.rightVc_?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
            
            leftVc_?.background = NSColor.clear
            leftVc_?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
            leftVc_?.background_act = KMAppearance.Interactive.m1Color()
            
            centerVc_?.background = NSColor.clear
            centerVc_?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
            centerVc_?.background_act = KMAppearance.Interactive.m1Color()
            
            rightVc_?.background = NSColor.clear
            rightVc_?.background_hov = NSColor(red: 71/255, green: 72/255, blue: 75/255, alpha: 1)
            rightVc_?.background_act = KMAppearance.Interactive.m1Color()
        } else {
            self.view.layer?.backgroundColor = .white
            self.leftVc_?.background_hov = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1)
            self.centerVc_?.background_hov = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1)
            self.rightVc_?.background_hov = NSColor(red: 237/255, green: 238/255, blue: 240/255, alpha: 1)
        }
    }
}