//
//  DispatchOrderController.swift
//  OrderScheduling
//
//  Created by 中道 on 2023/8/10.
//

import Foundation
import DDControlsKit_Private
import SnapKit
import RxSwift
import RxRelay
import DDAutoUIKit_Private
import DDMAMapKit_Private

fileprivate let dispatchPannelViewHeight = auto(300)
fileprivate let dispatchPannelViewTopInset = auto(45)

extension DispatchOrderController {
    func addActions() {
        
        let serviceAddressCoordinate = CLLocationCoordinate2D(latitude: serviceAddressLat, longitude: serviceAddressLon)
        let serviceAddressPointAnnotation = MAPointAnnotation.init()
        serviceAddressPointAnnotation.annotationClass = DispatchMapSericeAddressPointAnnotation.self
        serviceAddressPointAnnotation.coordinate = serviceAddressCoordinate
        serviceAddressPointAnnotation.tag = 10000
        dispatchOrderView.maMapView.maMapView.addAnnotation(serviceAddressPointAnnotation)
        dispatchOrderView.maMapView.maMapView.setCenter(serviceAddressCoordinate, animated: true)
        
        let bottomSafeArea = view.safeAreaInsets.bottom
        dispatchOrderView.tapGes.rx.event
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                self?.dispatchOrderView.coverView.isHidden = true
                
                self?.dispatchOrderView.dispatchPannelView.snp.updateConstraints({ make in
                    make.bottom.equalToSuperview().offset((dispatchPannelViewHeight - dispatchPannelViewTopInset) - bottomSafeArea)
                })
                
                UIView.animate(withDuration: 0.25) {[weak self] in
                    self?.dispatchOrderView.layoutIfNeeded()
                }
            })
            .disposed(by: disposeBag)
        
        dispatchOrderView.panGes.rx.event
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                self?.dispatchOrderView.coverView.isHidden = true
                
                self?.dispatchOrderView.dispatchPannelView.snp.updateConstraints({ make in
                    make.bottom.equalToSuperview().offset((dispatchPannelViewHeight - dispatchPannelViewTopInset) - bottomSafeArea)
                })
                
                UIView.animate(withDuration: 0.25) {[weak self] in
                    self?.dispatchOrderView.layoutIfNeeded()
                }
            })
            .disposed(by: disposeBag)
        
        dispatchOrderView.dispatchPannelView.onlineButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                if let isSelected = self?.dispatchOrderView.dispatchPannelView.onlineButton.isSelected,isSelected == false {
                    self?.dispatchOrderView.dispatchPannelView.onlineButton.isSelected = !isSelected
                    self?.dispatchOrderView.dispatchPannelView.offlineButton.isSelected = false
                    self?.dispatchOrderView.dispatchPannelView.tapBackgroundView.image = UIImage(named: "dispatchOrder_online_tap_background")
                    self?.dispatchOrderView.dispatchPannelView.tableView.isHidden = false
                    self?.dispatchOrderView.dispatchPannelView.offlineView.isHidden = true
                }
                
                self?.dispatchOrderView.coverView.isHidden = false

                self?.dispatchOrderView.dispatchPannelView.snp.updateConstraints({ make in
                    make.bottom.equalToSuperview().offset(0)
                })
                
                UIView.animate(withDuration: 0.25) {[weak self] in
                    self?.dispatchOrderView.layoutIfNeeded()
                }
            })
            .disposed(by: disposeBag)
        
        dispatchOrderView.dispatchPannelView.offlineButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                if let isSelected = self?.dispatchOrderView.dispatchPannelView.offlineButton.isSelected,isSelected == false {
                    self?.dispatchOrderView.dispatchPannelView.offlineButton.isSelected = !isSelected
                    self?.dispatchOrderView.dispatchPannelView.onlineButton.isSelected = false
                    self?.dispatchOrderView.dispatchPannelView.tapBackgroundView.image = UIImage(named: "dispatchOrder_offline_tap_background")
                    self?.dispatchOrderView.dispatchPannelView.tableView.isHidden = true
                    self?.dispatchOrderView.dispatchPannelView.offlineView.isHidden = false
                }
                
                self?.dispatchOrderView.coverView.isHidden = false

                self?.dispatchOrderView.dispatchPannelView.snp.updateConstraints({ make in
                    make.bottom.equalToSuperview().offset(0)
                })
                
                UIView.animate(withDuration: 0.25) {[weak self] in
                    self?.dispatchOrderView.layoutIfNeeded()
                }
            })
            .disposed(by: disposeBag)
        
        dispatchOrderView.dispatchPannelView.offlineView.rewriteButton.rx.tap
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] _ in
                self?.dispatchOrderView.dispatchPannelView.offlineView.name.textFiled.text = nil
                self?.dispatchOrderView.dispatchPannelView.offlineView.phone.textFiled.text = nil
                self?.dispatchOrderView.dispatchPannelView.offlineView.license.textFiled.text = nil
            })
            .disposed(by: disposeBag)
        
        dispatchOrderView.dispatchPannelView.offlineView.submitButton.rx.tap
            .filter({[weak self] _ in
                if self?.dispatchOrderView.dispatchPannelView.offlineView.name.textFiled.text?.isEmpty != false {
                    self?.view.dd_makeToast(dispatchOrderInputName)
                    return false
                }
                if self?.dispatchOrderView.dispatchPannelView.offlineView.phone.textFiled.text?.isEmpty != false {
                    self?.view.dd_makeToast(dispatchOrderInputPhone)
                    return false
                }
                if self?.dispatchOrderView.dispatchPannelView.offlineView.license.textFiled.text?.isEmpty != false {
                    self?.view.dd_makeToast(dispatchOrderInputLicense)
                    return false
                }
                return self?.dispatchOrderView.dispatchPannelView.offlineView.name.textFiled.text?.isEmpty == false && self?.dispatchOrderView.dispatchPannelView.offlineView.phone.textFiled.text?.isEmpty == false && self?.dispatchOrderView.dispatchPannelView.offlineView.license.textFiled.text?.isEmpty == false
            })
            .flatMapLatest {[weak self] _ in
                return RQ.dispatchVehicle(parameters: DispatchVehicleParameters(type: .offline, userOrderId: (self?.userOrderId)!, taskOrderId: (self?.taskOrderId)!,driverName: (self?.dispatchOrderView.dispatchPannelView.offlineView.name.textFiled.text)!,driverPhone: (self?.dispatchOrderView.dispatchPannelView.offlineView.phone.textFiled.text)!,plateNumber: (self?.dispatchOrderView.dispatchPannelView.offlineView.license.textFiled.text)!))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] response in
                self?.dispatchOrderResponse(response: response)
            })
            .disposed(by: disposeBag)
        
        dispatchOrderRelay
            .flatMapLatest {[weak self] model in
                return RQ.dispatchVehicle(parameters: DispatchVehicleParameters(type: .online, userOrderId: (self?.userOrderId)!, taskOrderId: (self?.taskOrderId)!,driverId: model.driverId,vehicleId: model.vehicleId))
            }
            .observe(on: MainScheduler.instance)
            .subscribe(onNext: {[weak self] response in
                self?.dispatchOrderResponse(response: response)
            })
            .disposed(by: disposeBag)
        
        RQ.dispatchVehicleList(parameters: DispatchVehicleListParameters(type:.online, userOrderId: userOrderId, taskOrderId: taskOrderId))
            .observe(on: MainScheduler.instance)
            .subscribe(onSuccess: {[weak self] response in
                if response?.success == true {
                    if let array = response?.data {
                        // 重新计算数组
                        self?.resultArr.removeAll()
                        self?.resultArr.append(contentsOf: array)
                        self?.dispatchOrderView.maMapView.maMapView.removeAnnotations(self?.vehicleAnnotions)
                        self?.vehicleAnnotions.removeAll()
                        
                        for index in 0..<array.count {
                            let model = array[index]
                            let coordinate = CLLocationCoordinate2D(latitude: Double(model.lat ?? "") ?? 0, longitude: Double(model.lon ?? "") ?? 0)
                            let pointAnnotation = MAPointAnnotation.init()
                            pointAnnotation.annotationClass = DispatchMapStatePointAnnotation.self
                            pointAnnotation.coordinate = coordinate
                            pointAnnotation.tag = index
                            self?.dispatchOrderView.maMapView.maMapView.addAnnotation(pointAnnotation)
                            self?.vehicleAnnotions.append(pointAnnotation)
                        }
                        
                        self?.dispatchOrderView.dispatchPannelView.tableView.reloadData()
                    }
                }else{
                    self?.view.dd_makeToast(response?.msg)
                }
            })
            .disposed(by: disposeBag)
        
        dispatchOrderView.dispatchPannelView.onlineButton.sendActions(for: .touchUpInside)
    }
    
    func dispatchOrderResponse(response:ResponseModel<String>?) {
        view.dd_makeToast(response?.msg ?? dispatchOrderSuccess,completion: {[weak self] _ in
            DispatchQueue.main.async {
                self?.navigationController?.popViewController(animated: true)
            }
            
            if self?.dispatchCompletionHandler != nil {
                self?.dispatchCompletionHandler?((self?.userOrderId)!)
            }
        })
    }
}

extension DispatchOrderController : UITableViewDelegate,UITableViewDataSource {
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return resultArr.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? DispatchOrderPannelCell
        if cell == nil {
            cell = DispatchOrderPannelCell.init(style: .default, reuseIdentifier: "cell")
        }
        let model = resultArr[indexPath.item]
        switch model.vehicleStatus.code {
        case .offLine,.lostConnection:
            cell?.stateLabel.textColor = .hex("787878")
            cell?.name.textColor = .hex("787878")
            cell?.phone.textColor = .hex("787878")
            cell?.distance.textColor = .hex("787878")
            break
        default:
            cell?.stateLabel.textColor = .hex("3364B7")
            cell?.name.textColor = .hex("3364B7")
            cell?.phone.textColor = .hex("3364B7")
            cell?.distance.textColor = .hex("3364B7")
            break
        }
        
        var status = model.vehicleStatus.label
        if model.onlineStatus.code == .lostConnection {
            status = status + ",掉"
        }
        cell?.stateLabel.text = (model.vehicleName ?? "")+"/"+status
        cell?.name.text = model.driverName
        cell?.phone.text = model.driverPhone
        if let distance = model.distance {
            cell?.distance.text = String(distance)+"km"
        }
        
        cell?.dispatchButton.rx.tap
            .subscribe(onNext: {[weak self] _ in
                self?.dispatchOrderRelay.accept(model)
            })
            .disposed(by: cell!.disposeBag)
        return cell!
    }
}

extension DispatchOrderController : DDMAMapViewDelegate {
    public func dd_mapView(_ mapView: MAMapView, viewFor annotation: MAAnnotation) -> MAAnnotationView {
        if annotation.isMember(of: MAPointAnnotation.self) {
            let pointAnnotation = annotation as! MAPointAnnotation
            if pointAnnotation.annotationClass == DispatchMapStatePointAnnotation.self{
                let DriverPointReuseIndentifier = "DispatchMapStatePointAnnotation"
                var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: DriverPointReuseIndentifier) as? DispatchMapStatePointAnnotation
                if annotationView == nil {
                    annotationView = DispatchMapStatePointAnnotation.init(annotation: pointAnnotation, reuseIdentifier: DriverPointReuseIndentifier)
                }
                annotationView!.annotation = pointAnnotation
                let model = resultArr[pointAnnotation.tag]
                annotationView?.stateLabel.text = model.vehicleStatus.label
                annotationView?.nameLabel.text = model.vehicleName

                switch model.vehicleStatus.code {
                case .offLine,.lostConnection:
                    annotationView?.pointView.backgroundColor = .hex("787878")
                    annotationView?.verticalView.backgroundColor = .hex("787878")
                    annotationView?.stateImageView.backgroundColor = .hex("787878")
                    break
                case .onLine,.leisure:
                    annotationView?.pointView.backgroundColor = .hex("2956DB")
                    annotationView?.verticalView.backgroundColor = .hex("2956DB")
                    annotationView?.stateImageView.backgroundColor = .hex("2956DB")
                    break
                case .busy:
                    annotationView?.pointView.backgroundColor = .hex("BA1717")
                    annotationView?.verticalView.backgroundColor = .hex("BA1717")
                    annotationView?.stateImageView.backgroundColor = .hex("BA1717")
                    break
                case .BuyBusy:
                    annotationView?.pointView.backgroundColor = .hex("ffa400")
                    annotationView?.verticalView.backgroundColor = .hex("ffa400")
                    annotationView?.stateImageView.backgroundColor = .hex("ffa400")
                    break
                }
                return annotationView!
            }
            
            if pointAnnotation.annotationClass == DispatchMapSericeAddressPointAnnotation.self{
                let DriverPointReuseIndentifier = "DispatchMapSericeAddressPointAnnotation"
                var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: DriverPointReuseIndentifier) as? DispatchMapSericeAddressPointAnnotation
                if annotationView == nil {
                    annotationView = DispatchMapSericeAddressPointAnnotation.init(annotation: pointAnnotation, reuseIdentifier: DriverPointReuseIndentifier)
                }
                annotationView!.annotation = pointAnnotation
                return annotationView!
            }
        }

        return MAAnnotationView.init(annotation: annotation, reuseIdentifier: "cell")
    }
    
    public func dd_mapView(_ mapView: MAMapView, didAnnotationViewTapped view: MAAnnotationView) {
        zIndex += 1
        view.zIndex = zIndex
    }
}

open class DispatchOrderController : DDViewController {
    private let disposeBag = DisposeBag()
    private let dispatchOrderView = DispatchOrderView()
    private let userOrderId : Int
    private let taskOrderId : Int
    private var resultArr : [DispatchVehicleListDataModel] = []
    private var vehicleAnnotions : [MAPointAnnotation] = []
    public var zIndex = 0
    private let serviceAddressLat : Double
    private let serviceAddressLon : Double
    private let dispatchOrderRelay = ReplayRelay<DispatchVehicleListDataModel>.create(bufferSize: 1)
    public var dispatchCompletionHandler: ((Int) -> Void)?
    public init(userOrderId:Int,taskOrderId:Int,serviceAddressLat:Double,serviceAddressLon:Double) {
        self.userOrderId = userOrderId
        self.taskOrderId = taskOrderId
        self.serviceAddressLat = serviceAddressLat
        self.serviceAddressLon = serviceAddressLon
        super.init(nibName: nil, bundle: nil)
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        let image = UIImage(named: "dispatchOrder_back")?.withRenderingMode(.alwaysOriginal)
        dd_backBarButtonItem?.image = image
        dd_navigationBarBackgroundColor = .clear
        addSubviews()
        addActions()
    }

    func addSubviews() {
        view.addSubview(dispatchOrderView)
        dispatchOrderView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        dispatchOrderView.maMapView.delegate = self
        dispatchOrderView.dispatchPannelView.tableView.delegate = self
        dispatchOrderView.dispatchPannelView.tableView.dataSource = self
    }
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .darkContent
    }
}

open class DispatchOrderPannelCell : DDTableViewCell {
    public let stateLabel : DDLabel
    public let name : DDLabel
    public let phone : DDLabel
    public let distance : DDLabel
    public let dispatchButton : DDButton
    public var dispatchLayer : CAGradientLayer = {
        var layer = CAGradientLayer.init()
        layer.startPoint = CGPoint(x: 0, y: 0)
        layer.endPoint = CGPoint(x: 1, y: 1)
        layer.locations = [0.0,1.0]
        layer.colors = [UIColor.hex("FF5A2C").cgColor,UIColor.hex("FE9D4D").cgColor]
        layer.cornerRadius = auto(4)
        layer.masksToBounds = true
        return layer
    }()
    public var disposeBag = DisposeBag()
    
    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        stateLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("3364B7"))
        name = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("3364B7"))
        phone = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("3364B7"))
        distance = DDLabel.dd_init(withText: "", font: .mediumFont(auto(13)), textColor: .hex("3364B7"))
        dispatchButton = DDButton.dd_initCustom()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        
//        stateLabel.lineBreakMode = .byTruncatingMiddle
        stateLabel.numberOfLines = 0
        contentView.addSubview(stateLabel)
        contentView.addSubview(name)
        contentView.addSubview(phone)
        distance.lineBreakMode = .byTruncatingMiddle
        contentView.addSubview(distance)
        dispatchButton.layer.cornerRadius = auto(4)
        dispatchButton.layer.masksToBounds = true
        dispatchButton.setTitle("派单", for: .normal)
        dispatchButton.titleLabel?.font = .mediumFont(auto(12))
        dispatchButton.layer.insertSublayer(dispatchLayer, at: 0)
        contentView.addSubview(dispatchButton)
        
        stateLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(10))
            make.centerY.equalToSuperview()
            make.width.equalTo(auto(95))
        }
        
        name.snp.makeConstraints { make in
            make.left.equalTo(stateLabel.snp.right).offset(auto(5))
            make.centerY.equalToSuperview()
            make.width.equalTo(auto(40))
        }
        
        phone.snp.makeConstraints { make in
            make.left.equalTo(name.snp.right).offset(auto(5))
            make.centerY.equalToSuperview()
            make.width.equalTo(auto(100))
        }
        
        distance.snp.makeConstraints { make in
            make.left.equalTo(phone.snp.right).offset(0)
            make.centerY.equalToSuperview()
            make.width.equalTo(auto(60))
        }
        
        dispatchButton.snp.makeConstraints { make in
            make.right.equalTo(-auto(10))
            make.width.equalTo(auto(50))
            make.height.equalTo(auto(20))
            make.centerY.equalToSuperview()
        }
    }
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        dispatchLayer.frame = CGRectMake(0, 0, auto(50), auto(20))
    }
    
    open override func prepareForReuse() {
        super.prepareForReuse()
        disposeBag = DisposeBag()
    }
}

open class DispatchOrderView : DDView {
    public let dispatchPannelView : DispatchOrderPannelView
    public let maMapView : DDMAMapView
    public let coverView : DDView
    public let tapGes : UITapGestureRecognizer
    public let panGes : UIPanGestureRecognizer
    public override init(frame: CGRect) {
        dispatchPannelView = DispatchOrderPannelView()
        maMapView = DDMAMapView()
        coverView = DDView()
        tapGes = UITapGestureRecognizer()
        panGes = UIPanGestureRecognizer()
        super.init(frame: frame)
        
        addSubview(maMapView)
        coverView.addGestureRecognizer(tapGes)
        coverView.addGestureRecognizer(panGes)
        addSubview(coverView)
        addSubview(dispatchPannelView)
        
        maMapView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        coverView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        dispatchPannelView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(dispatchPannelViewHeight)
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class DispatchOrderPannelView : DDView {
    private let radiusView : DDView
    public let tableView : DDTableView
    public let offlineView : DispatchOrderOfflineSchedulingView
    public let onlineButton : DDButton
    public let offlineButton : DDButton
    public let tapBackgroundView : DDImageView
    public override init(frame: CGRect) {
        radiusView = DDView()
        tableView = DDTableView.init(frame: CGRectZero, style: .plain)
        offlineView = DispatchOrderOfflineSchedulingView()
        onlineButton = DDButton.dd_initCustom()
        offlineButton = DDButton.dd_initCustom()
        tapBackgroundView = DDImageView.init()
        super.init(frame: frame)
        backgroundColor = .clear
        radiusView.backgroundColor = .clear
        addSubview(radiusView)
        tapBackgroundView.contentMode = .scaleAspectFill
        radiusView.addSubview(tapBackgroundView)
        onlineButton.setImage(UIImage(named: "dispatchOrder_online_unselected"), for: .normal)
        onlineButton.setImage(UIImage(named: "dispatchOrder_online_selected"), for: .selected)
        onlineButton.setTitle("线上调度", for: .normal)
        onlineButton.setTitle("线上调度", for: .selected)
        onlineButton.setTitleColor(.hex("585E6F").alpha(0.7), for: .normal)
        onlineButton.setTitleColor(.hex("000000"), for: .selected)
        onlineButton.titleLabel?.font = .mediumFont(auto(16))
        onlineButton.dd_customize(with: .ImageLeftPaddingTitleRightWithWholeCenter, padding: auto(10))
        radiusView.addSubview(onlineButton)
        offlineButton.setImage(UIImage(named: "dispatchOrder_offline_unselected"), for: .normal)
        offlineButton.setImage(UIImage(named: "dispatchOrder_offline_selected"), for: .selected)
        offlineButton.setTitle("线下调度", for: .normal)
        offlineButton.setTitle("线下调度", for: .selected)
        offlineButton.setTitleColor(.hex("585E6F").alpha(0.7), for: .normal)
        offlineButton.setTitleColor(.hex("000000"), for: .selected)
        offlineButton.titleLabel?.font = .mediumFont(auto(16))
        offlineButton.dd_customize(with: .ImageLeftPaddingTitleRightWithWholeCenter, padding: auto(10))
        radiusView.addSubview(offlineButton)
        tableView.backgroundColor = .white
        tableView.separatorStyle = .none
        radiusView.addSubview(tableView)
        radiusView.addSubview(offlineView)
        
        radiusView.layer.cornerRadius = auto(4)
        radiusView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(-safeAreaInsets.bottom)
        }
        
        tapBackgroundView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(-auto(20))
            make.height.equalTo(auto(65))
        }
        
        onlineButton.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(auto(45))
            make.width.equalToSuperview().multipliedBy(0.5)
            make.left.equalToSuperview()
        }
        
        offlineButton.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(auto(45))
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(tapBackgroundView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        offlineView.snp.makeConstraints { make in
            make.top.equalTo(tapBackgroundView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class DispatchOrderOfflineSchedulingView : DDView {
    public let name : DispatchOrderOfflineSchedulingItemView
    public let phone : DispatchOrderOfflineSchedulingItemView
    public let license : DispatchOrderOfflineSchedulingItemView
    public let rewriteButton : DDButton
    public let submitButton : DDButton
    override init(frame: CGRect) {
        name = DispatchOrderOfflineSchedulingItemView()
        phone = DispatchOrderOfflineSchedulingItemView()
        license = DispatchOrderOfflineSchedulingItemView()
        rewriteButton = DDButton.dd_initCustom()
        submitButton = DDButton.dd_initCustom()
        super.init(frame: frame)
        backgroundColor = .white
        name.titleLabel.text = "技师姓名"
        name.textFiled.attributedPlaceholder = NSAttributedString(string: dispatchOrderInputName,attributes: [.font:UIFont.mediumFont(auto(14)),.foregroundColor:UIColor.hex("A1A1A1")])
        addSubview(name)
        phone.titleLabel.text = "技师电话"
        phone.textFiled.attributedPlaceholder = NSAttributedString(string: dispatchOrderInputPhone,attributes: [.font:UIFont.mediumFont(auto(14)),.foregroundColor:UIColor.hex("A1A1A1")])
        addSubview(phone)
        license.titleLabel.text = "车牌号码"
        license.textFiled.attributedPlaceholder = NSAttributedString(string: dispatchOrderInputLicense,attributes: [.font:UIFont.mediumFont(auto(14)),.foregroundColor:UIColor.hex("A1A1A1")])
        addSubview(license)
        rewriteButton.layer.cornerRadius = auto(4)
        rewriteButton.layer.borderColor = UIColor.hex("DDDDDD").cgColor
        rewriteButton.layer.borderWidth = 1
        rewriteButton.setTitle("重新填写", for: .normal)
        rewriteButton.setTitleColor(.hex("323643").alpha(0.9), for: .normal)
        rewriteButton.titleLabel?.font = .mediumFont(auto(14))
        addSubview(rewriteButton)
        submitButton.layer.cornerRadius = auto(4)
        submitButton.backgroundColor = .hex("2A5094")
        submitButton.setTitle("确定提交", for: .normal)
        submitButton.setTitleColor(.white, for: .normal)
        submitButton.titleLabel?.font = .mediumFont(auto(14))
        addSubview(submitButton)
        
        name.snp.makeConstraints { make in
            make.top.equalTo(auto(30))
            make.left.right.equalToSuperview()
            make.height.equalTo(auto(40))
        }
        
        phone.snp.makeConstraints { make in
            make.top.equalTo(name.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(auto(40))
        }
        
        license.snp.makeConstraints { make in
            make.top.equalTo(phone.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(auto(40))
        }
        
        rewriteButton.snp.makeConstraints { make in
            make.right.equalTo(snp.centerX).offset(-auto(5))
            make.top.equalTo(license.snp.bottom).offset(auto(30))
            make.width.equalTo(auto(150))
            make.height.equalTo(auto(40))
        }
        
        submitButton.snp.makeConstraints { make in
            make.left.equalTo(snp.centerX).offset(auto(5))
            make.top.equalTo(rewriteButton)
            make.width.equalTo(auto(150))
            make.height.equalTo(auto(40))
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class DispatchOrderOfflineSchedulingItemView : DDView {
    public let titleLabel : DDLabel
    public let textFiled : DDTextField
    public let line : DDView
    override init(frame: CGRect) {
        titleLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(14)), textColor: .hex("323643").alpha(0.9))
        textFiled = DDTextField.init()
        line = DDView.init()
        super.init(frame: frame)
        
        addSubview(titleLabel)
        textFiled.textAlignment = .right
        textFiled.font = .mediumFont(auto(14))
        textFiled.textColor = .hex("323643").alpha(0.9)
        addSubview(textFiled)
        line.backgroundColor = .hex("F5F5F5")
        addSubview(line)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(auto(30))
            make.centerY.equalToSuperview()
        }
        
        textFiled.snp.makeConstraints { make in
            make.right.equalTo(-auto(30))
            make.centerY.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalTo(auto(30))
            make.right.equalTo(-auto(30))
            make.height.equalTo(1)
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class DispatchMapSericeAddressPointAnnotation : MAAnnotationView {
    private let topImageView : DDImageView
    private let bottonImageView : DDImageView
    
    override init!(annotation: MAAnnotation!, reuseIdentifier: String!) {
        topImageView = DDImageView(image: UIImage(named: "dispatchOrder_serviceAddress_top"))
        bottonImageView = DDImageView(image: UIImage(named: "dispatchOrder_serviceAddress_bottom"))
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        
        addSubview(bottonImageView)
        addSubview(topImageView)
        
        topImageView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(30)
            make.width.equalTo(20)
        }
        
        bottonImageView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.height.equalTo(15)
        }
        frame = CGRect(x: 0, y: 0, width: 20, height: 40)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class DispatchMapStatePointAnnotation : MAAnnotationView {
    public let radiusView : DDView
    public let stateImageView : DDImageView
    public let stateLabel : DDLabel
    public let nameLabel : DDLabel
    public let verticalView : UIView
    public let pointView : UIView

    override init!(annotation: MAAnnotation!, reuseIdentifier: String!) {
        radiusView = DDView.init()
        stateImageView = DDImageView.init()
        stateLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(10)), textColor: .white(alpha: 1.0))
        nameLabel = DDLabel.dd_init(withText: "", font: .mediumFont(auto(12)), textColor: .hex("#2C395F"))
        verticalView = UIView.init()
        pointView = UIView.init()
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        setUpSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpSubviews(){
        backgroundColor = .white
        frame = CGRect(x: 0, y: 0, width: auto(100), height: auto(30))
        layer.cornerRadius = auto(4)
        
        radiusView.backgroundColor = .white(alpha: 1)
        radiusView.layer.cornerRadius = auto(4)
        radiusView.layer.masksToBounds = true
        radiusView.isUserInteractionEnabled = false
        addSubview(radiusView)
        radiusView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.width.equalTo(auto(100))
            make.height.equalTo(auto(30))
        }
        
        stateImageView.isUserInteractionEnabled = false
        radiusView.addSubview(stateImageView)
        stateImageView.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.height.equalTo(auto(30))
        }
        
        stateLabel.textAlignment = .center
        stateLabel.isUserInteractionEnabled = false
        stateImageView.addSubview(stateLabel)
        stateLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        nameLabel.textAlignment = .center
        nameLabel.numberOfLines = 0
        nameLabel.isUserInteractionEnabled = false
        radiusView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(stateImageView.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(stateImageView.snp.centerY)
        }
        
        verticalView.isUserInteractionEnabled = false
        addSubview(verticalView)
        verticalView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(radiusView.snp.bottom)
            make.width.equalTo(auto(2.5))
            make.height.equalTo(auto(15))
        }
        
        pointView.isUserInteractionEnabled = false
        pointView.layer.cornerRadius = auto(5)
        pointView.layer.borderColor = UIColor.white.cgColor
        pointView.layer.borderWidth = auto(2)
        addSubview(pointView)
        pointView.snp.makeConstraints { make in
            make.centerX.equalTo(verticalView.snp.centerX)
            make.top.equalTo(verticalView.snp.bottom).offset(0)
            make.width.height.equalTo(auto(10))
        }
        
    }
}

