//
//  LNHelpCenterViewController.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/22.
//

import Foundation
import UIKit
import SnapKit


extension UIView {
    func pushToHelpCenter() {
        let vc = LNHelpCenterViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}


class LNHelpCenterViewController: LNViewController {
    private let maxUpload = 6
    private let messageInputView = LNCommonTextView()
    
    private let imageUploadView = LNMultiImagesUploadView()
    
    private let submitButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
    }
}

extension LNHelpCenterViewController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        updateSubmitButton()
    }
}

extension LNHelpCenterViewController {
    private func updateSubmitButton() {
        if !messageInputView.text.isEmpty {
            submitButton.setBackgroundImage(.primary_8, for: .normal)
            submitButton.isEnabled = true
        } else {
            submitButton.setBackgroundImage(nil, for: .normal)
            submitButton.isEnabled = false
        }
    }
    
    private func setupViews() {
        title = .init(key: "A00210")
        view.backgroundColor = .primary_1
        
        let scrollView = UIScrollView()
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let fakeView = UIView()
        scrollView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(0)
        }
        
        let container = UIView()
        container.backgroundColor = .fill
        container.layer.cornerRadius = 12
        scrollView.addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        let textView = buildTextView()
        container.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        let photoView = buildPhotoView()
        container.addSubview(photoView)
        photoView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(textView.snp.bottom).offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        let submit = buildSubmit()
        view.addSubview(submit)
        submit.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.bottom.equalToSuperview().offset(-view.safeBottomInset - 10)
        }
        
        view.onTap { [weak self] in
            guard let self else { return }
            view.endEditing(true)
        }
    }
    
    private func buildTextView() -> UIView {
        let container = UIView()
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00254")
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        titleLabel.numberOfLines = 0
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        messageInputView.delegate = self
        messageInputView.maxInput = LNReportManager.helpCenterMaxInput
        container.addSubview(messageInputView)
        messageInputView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.bottom.equalToSuperview()
            make.height.equalTo(150)
        }
        
        return container
    }
    
    private func buildPhotoView() -> UIView {
        let container = UIView()
        
        let titleLabel = UILabel()
        titleLabel.text = .init(key: "A00177", maxUpload)
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        let descLabel = UILabel()
        descLabel.text = .init(key: "A00178")
        descLabel.font = .body_s
        descLabel.textColor = .text_4
        container.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
        
        imageUploadView.maxPhoto = maxUpload
        container.addSubview(imageUploadView)
        imageUploadView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(descLabel.snp.bottom).offset(16)
            make.bottom.equalToSuperview()
            make.height.equalTo(0).priority(.low)
        }
        
        return container
    }
    
    private func buildSubmit() -> UIView {
        submitButton.clipsToBounds = true
        submitButton.backgroundColor = .fill_4
        submitButton.layer.cornerRadius = 23.5
        submitButton.setTitle(.init(key: "A00240"), for: .normal)
        submitButton.titleLabel?.font = .heading_h3
        submitButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            LNReportManager.shared.feedback(
                content: messageInputView.text,
                photos: imageUploadView.curFileUrls)
            { [weak self] success in
                guard let self else { return }
                guard success else { return }
                showToast(.init(key: "A00255"))
                navigationController?.popViewController(animated: true)
            }
        }), for: .touchUpInside)
        submitButton.snp.makeConstraints { make in
            make.height.equalTo(47)
        }
        
        return submitButton
    }
}


#if DEBUG

import SwiftUI

struct LNHelpCenterViewControllerPreview: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> some UIViewController {
        LNHelpCenterViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        
    }
}

#Preview(body: {
    LNHelpCenterViewControllerPreview()
})
#endif

