//
//  RoleDialogStyleSetView.swift

//
//  Created by Leon on 2025/7/21.
//
import UIKit
import Combine
class RoleDialogStyleSetView: UIView {
    var backButton: EPIconTertiaryButton!
    var bottomButton: StyleButton!
    var container: LTScrollContainer!
    var titleView: TitleView!
    var stepLabel: UILabel!

    var styleTextView: TitleTextView!
    var openingTextView: TitleTextView!
    var vocieSelectView: CLSelectView!
    
    var generate1Button: TextButton!
    var generate2Button: TextButton!
    
    // Data
    
    @Published var dialogStyleContent: String = ""
    @Published var dialogStyleAIGenerated: String = ""
    
    @Published var openingContent: String = ""
    @Published var openingAIGenerated: String = ""
    
    @Published var canGoNext: Bool = false
    
    @Published var selectVoice: VoiceModel?
    
    private var cancellables = Set<AnyCancellable>()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupData()
        setupEvent()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        backButton = {
            let v = EPIconTertiaryButton(radius: .round, iconSize: .large, iconCode: .arrowLeftBorder)
            let size = v.bgImageSize()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.bottom.equalToSuperview().offset(-UIWindow.safeAreaBottom * 0.5 - 16)
                make.size.equalTo(CGSize(width: 88, height: size.height))
            }
            return v
        }()

        bottomButton = {
            let v = StyleButton(type: .custom)
            v.primary(size: .large)
            addSubview(v)
            v.snp.makeConstraints { make in
                // make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.leading.equalTo(backButton.snp.trailing).offset(16)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                make.bottom.equalToSuperview().offset(-UIWindow.safeAreaBottom * 0.5 - 16)
            }
            return v
        }()
        container = {
            let v = LTScrollContainer()
            v.stack.spacing = 24
            v.stack.alignment = .leading
            addSubview(v)
            v.snp.makeConstraints { make in
                // make.top.equalTo(navigationView.snp.bottom)
                make.top.equalToSuperview().offset(UIWindow.navBarTotalHeight)
                make.leading.trailing.equalToSuperview()
                make.bottom.equalTo(bottomButton.snp.top).offset(-16)
            }
            return v
        }()

        titleView = {
            let v = TitleView()
            v.optionInnerBottomPadding = 0
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
            }
            return v
        }()

        stepLabel = {
            let v = UILabel()
            v.font = CLSystemToken.font(token: .tlm)
            v.textColor = .c.ctsn
            v.text = "3/4"
            titleView.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalTo(titleView.titleLabel)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()


        do{
            openingTextView = {
                let v = TitleTextView()
                v.maxLimit = 150
                v.minLimit = 10
                container.stack.addArrangedSubview(v)
                v.snp.makeConstraints { make in
                    make.leading.equalToSuperview().offset(CGFloat.lrs)
                    make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                }
                return v
            }()
            
            generate2Button = {
                let v = TextButton()
                v.text = "自动生成"
                openingTextView.addSubview(v)
                v.snp.makeConstraints { make in
                    make.trailing.equalToSuperview()
                    make.centerY.equalTo(openingTextView.titleLabel)
                    make.height.equalTo(40)
                }
                return v
            }()
        }
        
        do{
            styleTextView = {
                let v = TitleTextView()
                v.titleAppendOptionalLabel()
                v.maxLimit = 300
                v.minLimit = 10
                container.stack.addArrangedSubview(v)
                v.snp.makeConstraints { make in
                    make.leading.equalToSuperview().offset(CGFloat.lrs)
                    make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                }
                return v
            }()
            
            generate1Button = {
                let v = TextButton()
                v.text = "自动生成"
                styleTextView.addSubview(v)
                v.snp.makeConstraints { make in
                    make.trailing.equalToSuperview()
                    make.centerY.equalTo(styleTextView.titleLabel)
                    make.height.equalTo(40)
                }
                return v
            }()
        }

        vocieSelectView = {
            let v = CLSelectView()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()

        titleView.title = "Dialog"
        styleTextView.titleLabel.text = "Dialogue style"
        styleTextView.placeholder = "请描述角色的聊天方式，对话语气"
        styleTextView.errorMsg = "只能包含10-300字符"
        
        openingTextView.titleLabel.text = "Opening remarks"
        openingTextView.placeholder = "请描述角色的开场白"
        openingTextView.errorMsg = "只能包含10-150字符"
        vocieSelectView.titleLabel.text = "Voice"
        vocieSelectView.placeholder = "选择角色语音音色"
        bottomButton.setTitle("Next", for: .normal)
    }
    
    private func setupData(){
        
    }
    
    private func setupEvent(){
        $canGoNext.sink {[weak self] can in
            self?.bottomButton.isEnabled = can
        }.store(in: &cancellables)
        
        styleTextView.textDidChanged = {[weak self] textfield in
            self?.dialogStyleContent = textfield.text
        }
        
        openingTextView.textDidChanged = {[weak self] textfield in
            self?.openingContent = textfield.text
        }
        
        $dialogStyleContent.sink {[weak self] str in
            self?.styleTextView.defaultText = str
        }.store(in: &cancellables)
        
        $openingContent.sink {[weak self] str in
            self?.openingTextView.defaultText = str
        }.store(in: &cancellables)
        
        $dialogStyleAIGenerated.sink {[weak self] str in
            //self?.styleTextView.defaultText = str
            self?.dialogStyleContent = str
        }.store(in: &cancellables)
        
        $openingAIGenerated.sink {[weak self] str in
            //self?.openingTextView.defaultText = str
            self?.openingContent = str
        }.store(in: &cancellables)
        
        $selectVoice.sink {[weak self] timber in
            self?.vocieSelectView.contentStr = timber?.voiceDict.name
        }.store(in: &cancellables)
        
        Publishers.CombineLatest3($dialogStyleContent.prepend(""), $openingContent.prepend(""), $selectVoice.prepend(nil)).map{
            style, opening, voice in
            if opening.trimmed.count > 0, voice != nil{
                return true
            }
            return false
        }.assign(to: &$canGoNext)
        
        #if DEBUG
        testData()
        #endif
    }
    
    
    private func testData(){
        openingContent = "嗨，我是你的好朋友，随时准备为你提供帮助！不管你是有一个具体问题，还是只是想聊聊，都可以直接告诉我哦。我们开始吧 😊"
        dialogStyleContent = "" // 可以为空
        //"这个角色以自然、亲切而专业的语气进行对话。它能够快速理解用户的意图，回复逻辑清晰，表达简洁明了。语气不过于拘谨，也不过于随意，保持尊重同时具有一定的亲和力。当用户感到困惑或提出问题时，它总是耐心解释，适时提出引导性的建议，营造轻松、高效的沟通氛围。"
    }
}
