//
//  ChatPersonalSettingView.swift

//
//  Created by Leon on 2025/8/17.
//

import UIKit
import Combine
class ChatPersonalSetting: UIView{
    var container: LTScrollContainer!
    var titleView: TitleView!
    var nicknameTitleField: TitleTextField!
    var sexView : CLSelectView!
    var birthdaySelectView: CLSelectView!
    var whoImAmTextView: TitleTextView!
    
    @Published var nickname: String?
    @Published var birthdayStr: String?
    @Published var birthdayDate: Date?
    @Published var whoIm: String?
    
    @Published var editFlag = false
    
    private var cancellables = Set<AnyCancellable>()
    
    var data: IMChatSetting?
    
    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() {
        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(bottomView.snp.top)
                make.bottom.equalToSuperview()
            }
            return v
        }()
        
        titleView = {
            let v = TitleView()
            v.optionInnerBottomPadding = 0
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview()
            }
            return v
        }()
        
        nicknameTitleField = {
            let v = TitleTextField()
            container.stack.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
            }
            return v
        }()
        
        sexView = {
            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
        }()
        
        birthdaySelectView = {
            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
        }()
        
        whoImAmTextView = {
            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
        }()
        
        titleView.title = "My Chat Persona"

        sexView.titleLabel.text = "Sex"
        sexView.showForceNormalSupportMsg("*Gender cannot be changed")
        sexView.isEnabled = false
        
        nicknameTitleField.titleLabel.text = "nickname".localized()
        nicknameTitleField.placeholder = "Nickname"

        birthdaySelectView.titleLabel.text = "Birthday"
        birthdaySelectView.placeholder = "Birthday"
        
        whoImAmTextView.errorMsg = "只能包含10-300字符"
        whoImAmTextView.titleLabel.text = "Who I am"
        whoImAmTextView.placeholder = "Describe the character characteristics and scene settings of your role…"
    }
    
    private func setupData(){
        
    }
    
    private func setupEvent(){
        $nickname.sink { [weak self] str in
            self?.nicknameTitleField.textfield.text = str
        }.store(in: &cancellables)
        
        $birthdayDate.sink {[weak self] date in
            guard let validDate = date else{return}
            let years = Date().years(from:validDate)
            if years < 18{
                self?.birthdaySelectView.showErrorMsg("Must be at least 18 years old.")
            }else{
                self?.birthdaySelectView.hideErrorInfo()
            }
            let formated = validDate.toString(dateFormat: "dd MMM, yyyy")
            self?.birthdaySelectView.contentStr = formated
        }.store(in: &cancellables)
        
        $whoIm.sink {[weak self] str in
            self?.whoImAmTextView.defaultText = str
        }.store(in: &cancellables)
        
        nicknameTitleField.textfield.textPublisher.sink {[weak self] str in
            self?.nickname = str?.trimmed
            self?.editFlag = true
        }.store(in: &cancellables)
        
        whoImAmTextView.textView.textPublisher.sink { [weak self] str in
            self?.whoIm = str?.trimmed
            self?.editFlag = true
        }.store(in: &cancellables)
    }
    
    func config(_ data: IMChatSetting?){
        self.data = data
        guard let setting = data else{return}
        
        sexView.contentStr = (setting.sex ?? .noncomfirming).localizedText
        
        nickname = setting.nickname ?? ""
        if let birthday = setting.birthday{
            let date = Date.dateFromMilliseconds(birthday)
            birthdayDate = date
        }
        
        whoIm = setting.whoAmI
    }
}
