// // AutoRefundsViewController.swift // GME Remit // // Created by Mac on 11/20/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class AutoRefundsViewController: UIViewController { struct StringConstants { let headerTitleText = "how_much_would_you_like_to_refund".localized() let availableBalanceTitleText = "available_balance_text".localized() let refundChargeTitleTxty = "per_refund_bank_charge".localized() let navigationTitle = "auto_refund_title_text".localized() let yourRefundText = "you_refund_text".localized() let refundButtonText = "refund_text".localized() } @IBOutlet weak var backgroundView: UIView! @IBOutlet weak var countryBackgroundView: UIView! @IBOutlet weak var availableBalanceTitleLabel: UILabel! @IBOutlet weak var refundChargeTitleLabel: UILabel! @IBOutlet weak var yourRefundLabel: UILabel! @IBOutlet weak var headerTitleLabel: UILabel! @IBOutlet weak var refundButton: UIButton! @IBOutlet weak var availableBalanceLabel: UILabel! @IBOutlet weak var refundChargeLabel: UILabel! @IBOutlet weak var amountTextfield: GMENumberTextField! var autorefund: AutoRefund? { didSet { let availableBalance = (autorefund?.currentBalance ?? "0") + "(KRW)" let refundCharge = (autorefund?.chargeAmount ?? "10,000") + "(KRW)" self.availableBalanceLabel.text = availableBalance self.refundChargeLabel.text = refundCharge } } override func viewDidLoad() { super.viewDidLoad() setup() // Do any additional setup after loading the view. } private func setup() { setupNormalNavigation() self.backgroundView.layer.borderColor = UIColor.init(hex: "E0E0E0").cgColor self.backgroundView.layer.borderWidth = 0.5 self.backgroundView.layer.cornerRadius = 6 self.countryBackgroundView.layer.cornerRadius = 6 configureText() amountTextfield.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) headerTitleLabel.textColor = .themeRed refundButton.backgroundColor = .themeRed amountTextfield.textColor = .themeRed refundButton.layer.cornerRadius = 5 } @objc private func editingChanged(_ sender: UITextField) { amountTextfield.text = Utility.getCommaSeperatedString(numberString: amountTextfield.text!) } private func configureText() { self.availableBalanceTitleLabel.text = StringConstants().availableBalanceTitleText + " : " self.refundChargeTitleLabel.text = StringConstants().refundChargeTitleTxty + " : " self.yourRefundLabel.text = StringConstants().yourRefundText self.refundButton.setTitle(StringConstants().refundButtonText, for: UIControl.State.normal) self.headerTitleLabel.text = StringConstants().headerTitleText } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationItem.title = StringConstants().navigationTitle fetchInfo() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) amountTextfield.becomeFirstResponder() } override func viewWillDisappear(_ animated: Bool) { super.viewWillAppear(animated) view.endEditing(true) } private func fetchInfo() { self.showProgressHud() let username = Utility.getMyUserName() self.fetchAutoRefundInfo( username: username, success: { (autorefund) in self.autorefund = autorefund self.hideProgressHud() }, failure: { (error) in self.hideProgressHud() self.alertWithOk( type: .error, message: error.localizedDescription, okTitle: "ok_text".localized()) { self.navigationController?.popViewController(animated: true) } self.show(error: error.localizedDescription) } ) } @IBAction func refund(_ sender: Any) { guard let amount = self.amountTextfield.text?.stringRemovingComma() else { return } let minAmnt = self.autorefund?.minAmount ?? "0" if amount.isEmpty { self.show(error: "add refund amount") return } if !isValidRefundable(amount: amount) { let min = Utility.getCommaSeperatedString(numberString: minAmnt) ?? "" self.show(error: "Amount cannot be minimum than \(min)") return } let chargeAmt = self.autorefund?.chargeAmount ?? "" let userName = Utility.getMyUserName() let id = Utility.getMyId() self.showProgressHud() self.refund( amount: amount, userName: userName, chargeAmount: chargeAmt, userId: id, success: { (successMessage) in self.hideProgressHud() self.alertWithOk( type: .normal, message: successMessage?.message, title: "Success", okTitle: "Ok", okAction: { self.navigationController?.popViewController(animated: true) self.updateBalance(value: successMessage) }) }, failure: { (error) in self.hideProgressHud() self.show(error: error.localizedDescription) } ) } private func updateBalance(value: SuccessMessage?) { let balance = value?.extra ?? "" GMEDB.shared.user.set(balance, .availableBalance) let userInfo = [SideMenuNavigationNotifications.availableBalance : balance] NotificationCenter.default.post( name: self.getAvailableBalanceNotificationName(), object: nil, userInfo: userInfo ) } func getAvailableBalanceNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: SideMenuNavigationNotifications.availableBalance) } private func isValidRefundable(amount: String) -> Bool { let minRefundAmount = (self.autorefund?.minAmount ?? "0").replacingOccurrences(of: ",", with: "") guard let amt = Double(amount), let minRefundAmt = Double(minRefundAmount) else {return false} let result = amt >= minRefundAmt return result } func show(error: String) { self.alert(type: .error, message: error) } } extension AutoRefundsViewController: FetchAutoRefundInfo {} extension AutoRefundsViewController: RefundService {}