//
//  FeedBackVC.swift
//  SwiftProject
//
//  Created by Mr.Zhou on 2024/4/12.
//

import UIKit
import SVProgressHUD
class FeedBackVC: BaseController, UITextViewDelegate, UITextFieldDelegate {
    //问题意见标题Label
    private lazy var problemTitleLabel:UILabel = setLabel(NSLocalizedString("v1.5_problem/suggestion title", comment: ""), color: .init(r: 255, g: 255, b: 255, a: 0.8), font: .systemFont(ofSize: 16, weight: .medium))
    //问题意见文本框
    private lazy var problemTextView:UITextView = {
        let textView:UITextView = .init()
        textView.textColor = .white
        textView.font = .systemFont(ofSize: 14, weight: .medium)
        textView.text = ""
        textView.backgroundColor = .clear
        textView.delegate = self
        return textView
    }()
    //问题意见占位字符
    private lazy var problemPlaceholderLabel:UILabel = setLabel(NSLocalizedString("v1.5_problem/suggestion placeholder", comment: ""), color: .init(r: 255, g: 255, b: 255, a: 0.6), font: .systemFont(ofSize: 14, weight: .medium))
    //1号计数Label
    private lazy var oneNumbersLabel:UILabel = setLabel("0/500", color: .init(r: 255, g: 255, b: 255, a: 0.5), font: .systemFont(ofSize: 14, weight: .medium))
    //问联系信息标题Label
    private lazy var contactTitleLabel:UILabel = setLabel(NSLocalizedString("v1.5_contact information title", comment: ""), color: .init(r: 255, g: 255, b: 255, a: 0.8), font: .systemFont(ofSize: 16, weight: .medium))
    
    //联系信息输入框
    private lazy var contactTextField:UITextField = {
        let textField:UITextField = .init()
        textField.text = ""
        textField.textColor = .white
        textField.font = .systemFont(ofSize: 14, weight: .medium)
        textField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("v1.5_contact information placeholder", comment: ""), attributes: [.foregroundColor:UIColor(r: 255, g: 255, b: 255, a: 0.6), .font:UIFont.systemFont(ofSize: 14, weight: .medium)])
        textField.borderStyle = .none
        textField.delegate = self
        return textField
    }()
    //二号计数Label
    private lazy var twoNumbersLabel:UILabel = setLabel("0/50", color: .init(r: 255, g: 255, b: 255, a: 0.5), font: .systemFont(ofSize: 14, weight: .medium))
    //提交按钮
    private lazy var submitButton:UIButton = {
        let btn = UIButton()
        btn.setTitle(NSLocalizedString("v1.5_submit", comment: ""), for: .normal)
        btn.setTitle(NSLocalizedString("v1.5_submit", comment: ""), for: .disabled)
        btn.setTitleColor(.init(r: 255, g: 255, b: 255, a: 0.6), for: .disabled)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
        btn.addTarget(self, action: #selector(submitClick(_ :)), for: .touchUpInside)
        //不可选中颜色
        btn.backgroundColor = .init(r: 42, g: 42, b: 42, a: 1)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 12
        btn.isEnabled = false
        return btn
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        navLine?.isHidden = true
        setLeftOneBtnImg(imgStr: "menuvc_backbtn")
//        leftBtn1?.addTarget(self, action: #selector(popVC(_ :)), for: .touchUpInside)
        self.view.backgroundColor = UIColor(r: 6, g: 5, b: 7, a: 1)
        //设置标题
        titleLab?.isHidden = false
        setNavgaionTitle(string: NSLocalizedString("v1.5_feedback", comment: ""))
        setNavgationTitleAttribute(font: .systemFont(ofSize: 18, weight: .medium), color: .white)
        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(boardKeyClick(_ :))))
        configureUI()
    }

    //生成label
    private func setLabel(_ text:String, color:UIColor, font:UIFont) -> UILabel {
        let label = UILabel()
        label.text = text
        label.textColor = color
        label.font = font
        label.numberOfLines = 0
        return label
    }
    @objc private func boardKeyClick(_ sender:UITapGestureRecognizer) {
        view.endEditing(true)
    }
    //配置UI
    private func configureUI() {
        //配置问题/意见View
        let problemView = problemView()
        view.addSubview(problemView)
        problemView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(145)
            make.width.equalTo(345)
            make.height.equalTo(120)
        }
        view.addSubview(problemTitleLabel)
        problemTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(problemView.snp.left).offset(12)
            make.bottom.equalTo(problemView.snp.top).offset(-12)
        }
        let imageView = UIImageView(image: .init(named: "ProblemSuggestion description'logo"))
        view.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.equalTo(problemTitleLabel)
            make.left.equalTo(problemTitleLabel.snp.right)
        }
        //配置联系我们View
        let contactView = contactView()
        view.addSubview(contactView)
        contactView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(problemView.snp.bottom).offset(72)
            make.width.equalTo(345)
            make.height.equalTo(56)
        }
        view.addSubview(contactTitleLabel)
        contactTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(problemTitleLabel)
            make.bottom.equalTo(contactView.snp.top).offset(-10)
        }
        //添加提交按钮
        view.addSubview(submitButton)
        submitButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-52)
            make.width.equalTo(345)
            make.height.equalTo(54)
        }
    }
    //生成问题意见View
    private func problemView() -> UIView {
        let bgView = UIView()
        bgView.backgroundColor = .init(r: 29, g: 29, b: 29, a: 1)
        bgView.layer.masksToBounds = true
        bgView.layer.cornerRadius = 12
        bgView.addSubview(problemTextView)
        problemTextView.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(72)
        }
        bgView.addSubview(problemPlaceholderLabel)
        problemPlaceholderLabel.snp.makeConstraints { make in
            make.top.left.right.equalTo(problemTextView)
        }
        bgView.addSubview(oneNumbersLabel)
        oneNumbersLabel.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview().offset(-12)
        }
        return bgView
    }
    //生成联系我们View
    private func contactView() -> UIView {
        let bgView = UIView()
        bgView.backgroundColor = .init(r: 29, g: 29, b: 29, a: 1)
        bgView.layer.masksToBounds = true
        bgView.layer.cornerRadius = 8
        bgView.addSubview(twoNumbersLabel)
        twoNumbersLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-12)
        }
        bgView.addSubview(contactTextField)
        contactTextField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-42)
        }
        return bgView
    }
    //MARK: - textField And textView
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        guard text.count <= 50 else {
            twoNumbersLabel.text = "50/50"
            return false
        }
        twoNumbersLabel.text = "\(text.count)/50"
        return true
    }
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
            self.problemPlaceholderLabel.isHidden = true // 隐藏
            return true
        }
    func textViewDidBeginEditing(_ textView: UITextView) {
            
        }
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
            return true
        }
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            self.problemPlaceholderLabel.isHidden = false  // 显示
        }
        else{
            self.problemPlaceholderLabel.isHidden = true  // 隐藏
        }
    }
    func textViewDidChange(_ textView: UITextView) {
        
    }
    //获取textView字符内容
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let str = (textView.text! as NSString).replacingCharacters(in: range, with: text)
        guard str.count <= 500 else {
            oneNumbersLabel.text = "500/500"
            submitButton.isEnabled = true
            submitButton.backgroundColor = .init(r: 83, g: 38, b: 214, a: 1)
            return false
        }
        if str.count >= 1 {
            submitButton.isEnabled = true
            submitButton.backgroundColor = .init(r: 83, g: 38, b: 214, a: 1)
        }else {
            submitButton.isEnabled = false
            submitButton.backgroundColor = .init(r: 42, g: 42, b: 42, a: 1)
        }
        oneNumbersLabel.text = "\(str.count)/500"
        return true
    }
    //pop回上一个控制器
    @objc private func popVC(_ sender:UIButton) {
        navigationController?.popViewController(animated: true)
    }
    //提交
    @objc private func submitClick(_ sender:UIButton) {
        
        SVProgressHUD.show(withStatus: NSLocalizedString("v1.5_提交中...", comment: ""))
        //获取
        let probleText = problemTextView.text ?? ""
        let contact = contactTextField.text ?? ""
        
        //上传用户反馈的内容
        ZNetUtil.postUrl(urlStr: ZNetUtil.kUserFeedbackUrl, params: ["catalog":"feedback","remark":probleText,"contact":contact]) { [weak self] result, error in
            SVProgressHUD.dismiss()
            self?.navigationController?.popViewController(animated: true)
        }
    }
}
