//
//  RefuseOrderEntryView.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/9.
//

import Foundation
import DDControlsKit_Private
import DDAutoUIKit_Private
import RxSwift
import RxCocoa

extension RefuseOrderEntryView : UITextViewDelegate{
    func addActions() {
        timeView.selectButton.isSelected = true
        distanceTextFieldView.isHidden = true
        cannotDispatchTextViewView.isHidden = true
        
        timeView.selectButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                self?.timeView.selectButton.isSelected = true
                self?.timeTextFieldView.isHidden = false
                self?.distanceView.selectButton.isSelected = false
                self?.cannotDispatchView.selectButton.isSelected = false
                self?.distanceTextFieldView.isHidden = true
                self?.cannotDispatchTextViewView.isHidden = true
            })
            .disposed(by: disposeBag)
        
        timeTextFieldView.actionButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                CP.showItems(items: ["60到90","90到120","120以上"],selectedItem: self?.timeTextFieldView.textFiled.text) {[weak self] result in
                    if let result {
                        DispatchQueue.main.async {
                            self?.timeTextFieldView.textFiled.text = result.value
                        }
                    }
                }
            })
            .disposed(by: disposeBag)
        
        distanceView.selectButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                self?.distanceView.selectButton.isSelected = true
                self?.distanceTextFieldView.isHidden = false
                self?.timeView.selectButton.isSelected = false
                self?.cannotDispatchView.selectButton.isSelected = false
                self?.timeTextFieldView.isHidden = true
                self?.cannotDispatchTextViewView.isHidden = true
            })
            .disposed(by: disposeBag)
        
        cannotDispatchView.selectButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                self?.cannotDispatchView.selectButton.isSelected = true
                self?.cannotDispatchTextViewView.isHidden = false
                self?.timeView.selectButton.isSelected = false
                self?.distanceView.selectButton.isSelected = false
                self?.timeTextFieldView.isHidden = true
                self?.distanceTextFieldView.isHidden = true
            })
            .disposed(by: disposeBag)
    }
    
    public func textViewDidChange(_ textView: UITextView) {
        if textView.text.isEmpty {
            cannotDispatchTextViewView.placeHolder.isHidden = false
        }else {
            cannotDispatchTextViewView.placeHolder.isHidden = true
        }
    }
}

open class RefuseOrderEntryView :DDView {
    let radiusView : DDView
    let topView : DDView
    let titleLabel : DDLabel
    let exitButton : DDButton
    let seperateLine : DDView
    public let timeView : RefuseOrderEntryReasonView
    public let distanceView : RefuseOrderEntryReasonView
    public let cannotDispatchView : RefuseOrderEntryReasonView
    public let timeTextFieldView : RefuseOrderEntryTextFieldView
    public let distanceTextFieldView : RefuseOrderEntryTextFieldView
    public let cannotDispatchTextViewView : RefuseOrderEntryTextViewView
    let submitButton : DDButton
    let disposeBag : DisposeBag
    public override init(frame: CGRect) {
        radiusView = DDView()
        topView = DDView()
        titleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(16)), textColor: .hex("000000"))
        exitButton = DDButton.dd_initCustom()
        seperateLine = DDView()
        timeView = RefuseOrderEntryReasonView()
        distanceView = RefuseOrderEntryReasonView()
        cannotDispatchView = RefuseOrderEntryReasonView()
        timeTextFieldView = RefuseOrderEntryTextFieldView()
        distanceTextFieldView = RefuseOrderEntryTextFieldView()
        cannotDispatchTextViewView = RefuseOrderEntryTextViewView()
        submitButton = DDButton.dd_initCustom()
        disposeBag = DisposeBag()
        super.init(frame: frame)
        
        addSubview(radiusView)
        topView.backgroundColor = .hex("E3ECF1")
        radiusView.addSubview(topView)
        titleLabel.text = "拒单原因"
        topView.addSubview(titleLabel)
        exitButton.setImage(UIImage(named: "refuseOrder_exit"), for: .normal)
        exitButton.isHidden = true
        topView.addSubview(exitButton)
        topView.addSubview(seperateLine)
        timeView.titleLabel.text = "时间长"
        radiusView.addSubview(timeView)
        distanceView.isHidden = true
        distanceView.titleLabel.text = "距离远"
        radiusView.addSubview(distanceView)
        cannotDispatchView.titleLabel.text = "无法派出"
        radiusView.addSubview(cannotDispatchView)
        timeTextFieldView.layer.cornerRadius = auto(3)
        timeTextFieldView.layer.borderColor = UIColor.hex("3364B7").cgColor
        timeTextFieldView.layer.borderWidth = 1
        timeTextFieldView.titleLabel.text = "需要"
        timeTextFieldView.unitLabel.text = "分钟"
        timeTextFieldView.textFiled.isUserInteractionEnabled = false
        timeTextFieldView.actionButton.isHidden = false
        timeTextFieldView.backgroundColor = .hex("ECF4F9")
        radiusView.addSubview(timeTextFieldView)
        distanceTextFieldView.layer.cornerRadius = auto(3)
        distanceTextFieldView.layer.borderColor = UIColor.hex("3364B7").cgColor
        distanceTextFieldView.layer.borderWidth = 1
        distanceTextFieldView.backgroundColor = .hex("ECF4F9")
        distanceTextFieldView.titleLabel.text = "距离"
        distanceTextFieldView.unitLabel.text = "公里"
        radiusView.addSubview(distanceTextFieldView)
        cannotDispatchTextViewView.layer.cornerRadius = auto(3)
        cannotDispatchTextViewView.layer.borderColor = UIColor.hex("3364B7").cgColor
        cannotDispatchTextViewView.layer.borderWidth = 1
        cannotDispatchTextViewView.backgroundColor = .hex("ECF4F9")
        cannotDispatchTextViewView.textView.delegate = self
        radiusView.addSubview(cannotDispatchTextViewView)
        submitButton.layer.cornerRadius = auto(20)
        submitButton.backgroundColor = .hex("3364B7")
        submitButton.setTitle("提交", for: .normal)
        radiusView.addSubview(submitButton)
        
        radiusView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        topView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(auto(48))
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
        }
        
        exitButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-auto(20))
            make.centerY.equalToSuperview()
        }
        
        seperateLine.snp.makeConstraints { make in
            make.top.equalTo(topView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        distanceView.snp.makeConstraints { make in
            make.top.equalTo(seperateLine.snp.bottom).offset(auto(20))
            make.centerX.equalToSuperview()
            make.height.equalTo(30)
            make.width.equalTo(70)
        }
        
        timeView.snp.makeConstraints { make in
            make.centerY.equalTo(distanceView)
            make.right.equalTo(distanceView.snp.centerX).offset(-auto(30))
            make.width.height.equalTo(distanceView)
        }
        
        cannotDispatchView.snp.makeConstraints { make in
            make.centerY.equalTo(distanceView)
            make.left.equalTo(distanceView.snp.centerX).offset(auto(30))
            make.width.height.equalTo(distanceView)
        }
        
        timeTextFieldView.snp.makeConstraints { make in
            make.top.equalTo(distanceView.snp.bottom).offset(auto(20))
            make.centerX.equalToSuperview()
            make.width.equalTo(auto(265))
            make.height.equalTo(auto(44))
        }
        
        distanceTextFieldView.snp.makeConstraints { make in
            make.top.equalTo(distanceView.snp.bottom).offset(auto(20))
            make.centerX.equalToSuperview()
            make.width.equalTo(auto(265))
            make.height.equalTo(auto(44))
        }
        
        cannotDispatchTextViewView.snp.makeConstraints { make in
            make.top.equalTo(distanceView.snp.bottom).offset(auto(20))
            make.left.equalTo(auto(25))
            make.right.equalTo(-auto(25))
            make.height.equalTo(auto(90))
        }
        
        submitButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-auto(20))
            make.height.equalTo(auto(40))
            make.width.equalTo(auto(165))
        }
        
        addActions()
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class RefuseOrderEntryReasonView : DDView {
    public let selectButton : DDButton
    public let titleLabel : DDLabel
    public override init(frame: CGRect) {
        selectButton = DDButton.dd_initCustom()
        titleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(14)), textColor: .hex("3364B7"))
        super.init(frame: frame)
        selectButton.setImage(UIImage(named: "refuseOrder_reason_unselected"), for: .normal)
        selectButton.setImage(UIImage(named: "refuseOrder_reason_selected"), for: .selected)
        addSubview(selectButton)
        addSubview(titleLabel)
        selectButton.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(selectButton.snp.right).offset(5)
            make.centerY.equalToSuperview()
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class RefuseOrderEntryTextFieldView : DDView {
    public let titleLabel : DDLabel
    public let textFiled : DDTextField
    public let actionButton : DDButton
    public let unitLabel : DDLabel
    public override init(frame: CGRect) {
        titleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("3364B7"))
        textFiled = DDTextField()
        actionButton = DDButton.dd_initCustom()
        unitLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("000000"))
        super.init(frame: frame)
        addSubview(titleLabel)
        textFiled.textAlignment = .center
        textFiled.keyboardType = .decimalPad
        addSubview(textFiled)
        actionButton.isHidden = true
        addSubview(actionButton)
        addSubview(unitLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(16))
            make.centerY.equalToSuperview()
        }
        
        unitLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-auto(15))
            make.centerY.equalToSuperview()
        }
        
        textFiled.snp.makeConstraints { make in
            make.centerY.centerX.equalToSuperview()
            make.width.equalTo(auto(150))
            make.height.equalTo(auto(50))
        }
        actionButton.snp.makeConstraints { make in
            make.edges.equalTo(textFiled)
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class RefuseOrderEntryTextViewView : DDView {
    public let textView : DDTextView
    public let placeHolder : DDLabel
    public override init(frame: CGRect) {
        textView = DDTextView()
        placeHolder = DDLabel.dd_init(withText: "无法派出原因", font: .regularFont(auto(12)), textColor: .hex("3364B7").alpha(0.6))
        super.init(frame: frame)
        layer.cornerRadius = auto(3)
        layer.borderColor = UIColor.hex("ECF4F9").cgColor
        layer.borderWidth = 1
        addSubview(textView)
        textView.addSubview(placeHolder)
        textView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        placeHolder.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(auto(10))
            make.top.equalToSuperview().offset(7)
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
