//
//  DateDropdownEntryField.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 11/13/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit


@objcMembers open class DateDropdownEntryField: BaseDropdownEntryField {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public weak var datePicker: UIDatePicker?
    
    private var calendar: Calendar = {
        var calendar: Calendar = .current
        calendar.timeZone = NSTimeZone.system
        return calendar
    }()
    
    public var dateFormat: String? {
        get { dateDropdownModel?.dateFormat }
        set {
            guard let newValue = newValue else { return }
            dateDropdownModel?.dateFormat = newValue
        }
    }
    
    public var dateDropdownModel: DateDropdownEntryFieldModel? {
        model as? DateDropdownEntryFieldModel
    }
    
    //--------------------------------------------------
    // MARK: - Initializers
    //--------------------------------------------------
    
    @objc public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    @objc public convenience init() {
        self.init(frame: .zero)
    }
    
    @objc public convenience init(startDate: Date, endDate: Date, showStartDate: Bool = true) {
        self.init(frame: .zero)
        setDatePickerDuration(from: startDate, to: endDate, showStartDate: showStartDate)
    }
    
    @objc required public init?(coder: NSCoder) {
        fatalError("DateDropdownEntryField init(coder:) has not been implemented")
    }
    
    required public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.init(model: model, delegateObject, additionalData)
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    public override func setupFieldContainerContent(_ container: UIView) {
        super.setupFieldContainerContent(container)
        
        datePicker = UIDatePicker.addDatePicker(to: textField)
        datePicker?.addTarget(self, action: #selector(pickerValueChanged), for: .valueChanged)
        datePicker?.timeZone = NSTimeZone.system
        textField.inputView = datePicker
        UIToolbar.addDismissToolbar(to: textField, delegate: self, action: #selector(dismissFieldInput))
    }
    
    @objc public func setDatePickerDuration(from startDate: Date?, to endDate: Date?, showStartDate: Bool = true) {
        
        datePicker?.minimumDate = startDate
        datePicker?.maximumDate = endDate
        
        if showStartDate {
            setTextWith(date: startDate)
        }
    }
    
    @objc public func dismissDatePicker() -> Date? {
        
        let pickedDate = datePicker?.date
        setTextWith(date: pickedDate)
        resignFirstResponder()
        return pickedDate
    }
    
    @objc private func setTextWith(date: Date?) {
        
        guard let date = date else { return }
        
        dateDropdownModel?.date = date
        
        if calendar.isDate(date, inSameDayAs: Date()) {
            text = MVMCoreUIUtility.hardcodedString(withKey: "textfield_today_string")
        } else {
            text = dateDropdownModel?.dateFormatter.string(from: date)
        }
    }
    
    @objc public override func dismissFieldInput(_ sender: Any?) {
        
        setTextWith(date: datePicker?.date)
        super.dismissFieldInput(sender)
    }
    
    @objc func pickerValueChanged(_ sender: UIDatePicker) {
        
        setTextWith(date: datePicker?.date)
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? DateDropdownEntryFieldModel else { return }
        
        if let date = model.date {
            datePicker?.date = date
            setTextWith(date: datePicker?.date)
        }
        
        if let minDate = model.minDate {
            datePicker?.minimumDate = minDate
        }
        
        if let maxDate = model.maxDate {
            datePicker?.maximumDate = maxDate
        }
    }
    
    public override func reset() {
        super.reset()
        
        datePicker?.minimumDate = nil
        datePicker?.maximumDate = nil
        datePicker?.date = Date()
        text = nil
    }
}
