//
//  CheckImageButton.swift
//  hulan
//
//  Created by Taeyong An on 01/11/2018.
//  Copyright © 2018 Taeyong An. All rights reserved.
//

import Foundation
import UIKit
import ReactiveSwift
import ReactiveCocoa

struct AssociatedKeys {
    static var isChecked: Bool = false
    static var state: String = ""
    static var checkOnImg: String = ""
    static var checkOffImg: String = ""
}

class CheckImageButton: UIImageView {
    var isChecked: Bool {
        get {
            guard let value = objc_getAssociatedObject(self, &AssociatedKeys.isChecked) as? Bool else {
                return false
            }
            return value
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.isChecked, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
            if newValue, self.checkOnImg != ""{
                self.image = UIImage(named: self.checkOnImg)
            }else if self.checkOffImg != ""{
                self.image = UIImage(named: self.checkOffImg)
            }
        }
    }
    
    var state: String {
        get {
            guard let value = objc_getAssociatedObject(self, &AssociatedKeys.state) as? String else {
                return ""
            }
            return value
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.state, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var checkOnImg:String{
        get {
            guard let value = objc_getAssociatedObject(self, &AssociatedKeys.checkOffImg) as? String else {
                return ""
            }
            return value
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.checkOffImg, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var checkOffImg:String{
        get {
            guard let value = objc_getAssociatedObject(self, &AssociatedKeys.checkOnImg) as? String else {
                return ""
            }
            return value
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedKeys.checkOnImg, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    func toggle(){
        self.isChecked = !self.isChecked
    }
    
    @IBInspectable
    var checked: Bool {
        get {
            return self.isChecked
        }
        set {
            self.isChecked = newValue
        }
    }
    
    @IBInspectable
    var status: String {
        get {
            return self.state
        }
        set {
            self.state = newValue
        }
    }
    
    @IBInspectable
    var checkOn: String {
        get {
            return self.checkOnImg
        }
        set {
            self.checkOnImg = newValue
        }
    }
    
    @IBInspectable
    var checkOff: String {
        get {
            return self.checkOffImg
        }
        set {
            self.checkOffImg = newValue
        }
    }
}

extension Reactive where Base: CheckImageButton {
    var checked:BindingTarget<Bool> {
        return makeBindingTarget { $0.checked = $1 }
    }
}


