//
//  TSLightShapeVC.swift
//  Girly
//
//  Created by 100Years on 2025/1/7.
//

import JXSegmentedView

class TSLightShapeVC: TSBaseVC{
    
    lazy var currentColor:UIColor =  {
        let color = UIColor.white
        return color
    }()
    
    lazy var brightnessNum:CGFloat =  {
        return 1.0
    }(){
        didSet{
            updateColorView()
        }
    }
    
    lazy var saturationNum:CGFloat =  {
        return 1.0
    }(){
        didSet{
            updateColorView()
        }
    }
    
    lazy var colorPaletteView: TSLightColorPaletteView = {
        let colorPaletteView = TSLightColorPaletteView()
        colorPaletteView.changedBrightnessComplete = { [weak self] floatNum in
            guard let self = self else { return }
            brightnessNum = floatNum
        }
        
        colorPaletteView.changedSaturationComplete = { [weak self] floatNum in
            guard let self = self else { return }
            saturationNum = floatNum
        }
        
        return colorPaletteView
    }()
    
    lazy var shapeView: TSLightShapeView = {
        let shapeView = TSLightShapeView()
        return shapeView
    }()
    
    lazy var topView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        
        view.addSubview(shapeView)
        shapeView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(-60)
        }
        
        view.addSubview(colorPaletteView)
        colorPaletteView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.trailing.equalTo(-12)
            make.height.equalTo(100.0)
            make.bottom.equalTo(-12)
        }

        let fullScreenBtn = UIButton.createButton(image: UIImage(named: "fullScreen"),backgroundColor: UIColor.fromHex("000000", alpha: 0.2),corner: 8.0) { [weak self]  in
            guard let self = self else { return }
            kPresentModalVC(target: self, modelVC: TSFullScreenVC.creatShapeVC(color: shapeView.color, shapeNamed: shapeView.imageNamed),transitionStyle: .crossDissolve)
        }
        view.addSubview(fullScreenBtn)
        fullScreenBtn.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.trailing.equalTo(-28)
            make.width.height.equalTo(36)
        }
 
        return view
    }()
    
    lazy var toolView: TSLightShapeToolView = {
        let toolView = TSLightShapeToolView()
        toolView.changedColorComplete = {[weak self] color in
            guard let self = self else { return }
            currentColor = color
            shapeView.color = color
        }
        
        toolView.changedModelComplete = {[weak self] model in
            guard let self = self else { return }
            shapeView.imageNamed = model.image
        }
        return toolView
    }()

    override func createView() {
        setNavBarViewHidden(true)
        contentView.addSubview(topView)
        contentView.addSubview(toolView)
        topView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(kLightTop)
            make.bottom.equalTo(-172.0)
        }
        
        toolView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(128.0)
            make.bottom.equalTo(0)
        }
        self.view.layoutIfNeeded()
        
        kDelayMainShort {
            self.shapeView.addContent()
        }

    }
    
    
    func updateColorView(){
        shapeView.color = currentColor.adjusted(brightness: brightnessNum,saturation: saturationNum)
    }
}

extension TSLightShapeVC: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
