//
//  YHWorkExperienceItemView.swift
//  galaxy
//
//  Created by EDY on 2024/2/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHWorkExperienceItemView: UIView {
    typealias ItemBlock = (_ model: YHWorkExperienceModel) -> ()
    var block: ItemBlock?
    var titleLabel: UILabel!
    var messageTextField: UITextField!
    var nextStepImageView: UIImageView!
    var centerButton: UIButton!
    var leftButton: UIButton!
    var rightButton: UIButton!
    
    var dataSource: YHWorkExperienceModel? {
        didSet {
            updateAllViews()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpView() {
        titleLabel = {
            let label = UILabel()
            label.numberOfLines = 0
            return label
        }()
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(112)
        }
        
        messageTextField = {
            let textField = UITextField()
            textField.delegate = self
            return textField
        }()
        addSubview(messageTextField)
        messageTextField.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(112)
        }
        
        nextStepImageView = {
            let imageView = UIImageView()
            imageView.image = UIImage(named: "form_right_arrow")
            return imageView
        }()
        addSubview(nextStepImageView)
        nextStepImageView.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.width.equalTo(20)
        }
        
        centerButton = {
            let button = UIButton(type: .custom)
            button.addTarget(self, action: #selector(centerClick), for: .touchUpInside)
            return button
        }()
        addSubview(centerButton)
        centerButton.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(112)
        }
        
        rightButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
            button.setTitleColor(UIColor.brandMainColor, for: .selected)
            button.setTitleColor(UIColor.mainTextColor, for: .normal)
            button.layer.cornerRadius = 16
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.brandMainColor.cgColor
            button.isSelected = true
            button.addTarget(self, action: #selector(rightClick), for: .touchUpInside)
            return button
        }()
        addSubview(rightButton)
        rightButton.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(32)
            make.width.equalTo(74)
        }
        
        leftButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor.contentBkgColor
            button.setTitleColor(UIColor.brandMainColor, for: .selected)
            button.setTitleColor(UIColor.mainTextColor, for: .normal)
            button.layer.cornerRadius = 16
            button.addTarget(self, action: #selector(leftClick), for: .touchUpInside)
            return button
        }()
        addSubview(leftButton)
        leftButton.snp.makeConstraints { make in
            make.right.equalTo(rightButton.snp.left).offset(-12)
            make.centerY.equalToSuperview()
            make.height.equalTo(32)
            make.width.equalTo(74)
        }
        
    }
    
    func updateAllViews() {
        guard let dataSource = dataSource else { return }
        if dataSource.isNeed ?? false {
            let str = "*" + (dataSource.title ?? "")
            let attributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.PFSC_R(ofSize: 14),
                .foregroundColor: UIColor.mainTextColor
            ]
            let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
            let starRange = NSRange(location: 0, length: 1)
            questionAttrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex:0xFF3A3A), range: starRange)
            titleLabel.attributedText = questionAttrStr
        } else {
            let str = dataSource.title ?? ""
            let attributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.PFSC_R(ofSize: 14),
                .foregroundColor: UIColor.mainTextColor
            ]
            let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
            titleLabel.attributedText = questionAttrStr
        }
        messageTextField.placeholder = dataSource.prompts
        messageTextField.text = dataSource.message
        if dataSource.isUserKeyBoard ?? false {
            nextStepImageView.isHidden = true
            centerButton.isHidden = true
        } else {
            if let type = dataSource.type {
                switch type {
                case .normal:
                    break
                case .unit:
                    break
                case .nature:
                    break
                case .country:
                    break
                case .address:
                    break
                case .time:
                    break
                case .certificate:
                    break
                case .structure:
                    let arr = YHOtherInfoFillViewModel.arrStructure
                    arr.forEach { item in
                        if item["id"] == dataSource.message {
                            messageTextField.text = item["title"]
                        }
                    }
                case .level:
                    let arr = YHOtherInfoFillViewModel.arrLevel
                    arr.forEach { item in
                        if item["id"] == dataSource.message {
                            messageTextField.text = item["title"]
                        }
                    }
                case .prove:
                    break
                }
            }
            nextStepImageView.isHidden = false
            centerButton.isHidden = false
        }
       
        if (dataSource.leftButtonString != nil) {
            leftButton.isHidden = false
            leftButton.setTitle(dataSource.leftButtonString, for: .normal)
            nextStepImageView.isHidden = true
            messageTextField.isHidden = true
            if dataSource.message == "0" {
                buttonState(isLeft: false)
            } else {
                buttonState(isLeft: true)
            }
        } else {
            leftButton.isHidden = true
        }
        
        if (dataSource.rightButtonString != nil) {
            rightButton.isHidden = false
            rightButton.setTitle(dataSource.rightButtonString, for: .normal)
            nextStepImageView.isHidden = true
            messageTextField.isHidden = true
        } else {
            rightButton.isHidden = true
        }
    }
    
    func buttonState(isLeft: Bool) {
        if isLeft {
            leftButton.isSelected = true
            rightButton.isSelected = false
            leftButton.layer.borderWidth = 1
            leftButton.layer.borderColor = UIColor.brandMainColor.cgColor
            leftButton.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
            rightButton.backgroundColor = UIColor.contentBkgColor
            rightButton.layer.borderWidth = 0
        } else {
            rightButton.isSelected = true
            leftButton.isSelected = false
            rightButton.layer.borderWidth = 1
            rightButton.layer.borderColor = UIColor.brandMainColor.cgColor
            rightButton.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
            leftButton.backgroundColor = UIColor.contentBkgColor
            leftButton.layer.borderWidth = 0
        }
    }
    
    @objc func rightClick() {
        rightButton.isSelected = true
        leftButton.isSelected = false
        rightButton.layer.borderWidth = 1
        rightButton.layer.borderColor = UIColor.brandMainColor.cgColor
        rightButton.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
        leftButton.backgroundColor = UIColor.contentBkgColor
        leftButton.layer.borderWidth = 0
        dataSource?.message = "0"
        dataSource?.value = ["0"]
        if let block = block {
            block(dataSource ?? YHWorkExperienceModel())
        }
    }
    
    @objc func leftClick() {
        leftButton.isSelected = true
        rightButton.isSelected = false
        leftButton.layer.borderWidth = 1
        leftButton.layer.borderColor = UIColor.brandMainColor.cgColor
        leftButton.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
        rightButton.backgroundColor = UIColor.contentBkgColor
        rightButton.layer.borderWidth = 0
        dataSource?.message = "1"
        dataSource?.value = ["1"]
        if let block = block {
            block(dataSource ?? YHWorkExperienceModel())
        }
    }
    
    @objc func centerClick() {
        guard let type = dataSource?.type else { return }
        switch type {
        case .normal:
            printLog("不需要跳转")
        case .address:
            let addressPicker = YHAddressViewController(selectProvince: "上海市", selectCity: "" )
            addressPicker.backLocationStringController = { (address,province,city,area) in
                self.dataSource?.message = address
                self.dataSource?.value = [province, city, area]
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
            UIViewController.current?.present(addressPicker, animated: true, completion: nil)
        case .time:
            YHDatePickView.show(type: .yyyymm) { date in
                self.dataSource?.message = date
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
        case .country:
            let vc = YHSelectCountryViewController()
            vc.backLocationStringController = { (country) in
                self.dataSource?.message = country
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
            UIViewController.current?.navigationController?.pushViewController(vc)
        case .unit:
            let vc = YHCompanySelectViewController()
            vc.backLocationStringController = { (country) in
                self.dataSource?.message = country
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
            UIViewController.current?.navigationController?.pushViewController(vc)
        case .nature:
            YHOtherPickerView.show(type: .nature) {[weak self] data in
                guard let self = self else { return }
                self.dataSource?.message = data.title
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
        case .certificate:
            YHOtherPickerView.show(type: .certificate) {[weak self] data in
                guard let self = self else { return }
                self.dataSource?.message = data.title
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
        case .structure:
            YHOtherPickerView.show(type: .structure) {[weak self] data in
                guard let self = self else { return }
                self.dataSource?.message = data.title
                self.dataSource?.value = [data.index]
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
        case .level:
            YHOtherPickerView.show(type: .level) {[weak self] data in
                guard let self = self else { return }
                self.dataSource?.message = data.title
                self.dataSource?.value = [data.index]
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
        case .prove:
            YHOtherPickerView.show(type: .prove) {[weak self] data in
                guard let self = self else { return }
                self.dataSource?.message = data.title
                self.updateAllViews()
                if let block = self.block {
                    block(self.dataSource ?? YHWorkExperienceModel())
                }
            }
        }
    }
}

extension YHWorkExperienceItemView: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        dataSource?.message = textField.text
        if let block = block {
            block(dataSource ?? YHWorkExperienceModel())
        }
    }
}
