// Copyright 2017-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import UIKit

class ZShadow: UIView {
  @IBOutlet weak var greenTappable: MaterialShadowedView!
  @IBOutlet weak var greenBanner: MaterialShadowedView!
  
  @IBOutlet weak var blueTappable: VanillaShadowedView!
  @IBOutlet weak var blueBanner: VanillaShadowedView!
  
  @IBOutlet var containerView: UIView!

  override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
  }
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
  }
  
  func commonInit() {
    let bundle = Bundle(for: ZShadow.self)
    bundle.loadNibNamed("ZShadow", owner: self, options: nil)
    addSubview(containerView)
    self.backgroundColor = .white
    containerView.backgroundColor = .white
    containerView.frame = self.bounds
    containerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  }
}

class ZShadowViewController: UIViewController {
  
  var greenBannerLeadingConstraintCollapsed: NSLayoutConstraint!
  var blueBannerLeadingConstraintCollapsed: NSLayoutConstraint!
  var contentView: ZShadow!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    contentView = ZShadow(frame: self.view.bounds)
    self.view.addSubview(contentView)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|",
                                                       options: [],
                                                       metrics: nil,
                                                       views: ["view": contentView]));
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|",
                                                       options: [],
                                                       metrics: nil,
                                                       views: ["view": contentView]));
    if #available(iOS 9.0, *) {
      greenBannerLeadingConstraintCollapsed = contentView.greenBanner.trailingAnchor.constraint(equalTo: contentView.greenTappable.trailingAnchor)
    } else {
      greenBannerLeadingConstraintCollapsed = NSLayoutConstraint(item: contentView.greenBanner, attribute: .right, relatedBy: .equal, toItem: contentView.greenTappable, attribute: .right, multiplier: 1, constant: 0)
    }
    greenBannerLeadingConstraintCollapsed.isActive = false
    
    if #available(iOS 9.0, *) {
      blueBannerLeadingConstraintCollapsed = contentView.blueBanner.trailingAnchor.constraint(equalTo: contentView.blueTappable.trailingAnchor)
    } else {
      blueBannerLeadingConstraintCollapsed = NSLayoutConstraint(item: contentView.blueBanner, attribute: .right, relatedBy: .equal, toItem: contentView.blueTappable, attribute: .right, multiplier: 1, constant: 0)
    }
    blueBannerLeadingConstraintCollapsed.isActive = false
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(squaresTapped))
    contentView.greenTappable.addGestureRecognizer(tap)
    let tap2 = UITapGestureRecognizer(target: self, action: #selector(squaresTapped))
    contentView.blueTappable.addGestureRecognizer(tap2)
    
  }
  
  func squaresTapped() {
    greenBannerLeadingConstraintCollapsed.isActive = !greenBannerLeadingConstraintCollapsed.isActive
    blueBannerLeadingConstraintCollapsed.isActive = !blueBannerLeadingConstraintCollapsed.isActive
    
    UIView.animate(withDuration: 5, animations: {
      self.view.layoutIfNeeded()
    })
  }
}

extension ZShadowViewController {
  
  // MARK: Catalog by convention

  class func catalogMetadata() -> [String: Any] {
    return [
      "breadcrumbs": ["ZShadow"],
      "primaryDemo": false,
      "presentable": false,
    ]
  }
}

