//
//  UIView+SSAdd.swift
//  htcm_swift
//
//  Created by soldoros on 2018/12/20.
//  Copyright © 2018年 soldoros. All rights reserved.
//

import Foundation
import UIKit

extension UIView{
    
    //获取当前视图的控制器
    public func viewController()->UIViewController? {
        
        var nextResponder: UIResponder? = self
        
        repeat {
            nextResponder = nextResponder?.next
            
            if let viewController = nextResponder as? UIViewController {
                return viewController
            }
            
        } while nextResponder != nil
        
        return nil
    }

    
    
    public var left: CGFloat{
        get{
            return self.frame.origin.x;
        }
        set{
            var frame = self.frame;
            frame.origin.x = newValue;
            self.frame = frame;
        }
    }
    
    public var top: CGFloat{
        get{
            return self.frame.origin.y;
        }
        set{
            var frame = self.frame;
            frame.origin.y = newValue;
            self.frame = frame;
        }
    }
    
    public var right: CGFloat{
        get{
            return self.frame.origin.x + self.frame.size.width;
        }
        set{
            var frame = self.frame;
            frame.origin.x = newValue - frame.size.width;
            self.frame = frame;
        }
    }
    
    public var bottom: CGFloat{
        get{
            return self.frame.origin.y + self.frame.size.height;
        }
        set{
            var frame = self.frame;
            frame.origin.y = newValue - frame.size.height;
            self.frame = frame;
        }
    }
    
    
    public var width: CGFloat{
        get{
            return self.frame.size.width
        }
        set{
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
    }
    
    
    public var height: CGFloat{
        get{
            return self.frame.size.height
        }
        set{
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
    }
    
    
    public var centerX : CGFloat{
        get{
            return self.center.x
        }
        set{
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    public var centerY : CGFloat{
        get{
            return self.center.y
        }
        set{
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    
    
    public var origin: CGPoint{
        get{
            return self.frame.origin
        }
        set{
            var frame = self.frame;
            frame.origin = newValue;
            self.frame = frame;
        }
    }
    
    public var size: CGSize{
        get{
            return self.frame.size
        }
        set{
            var frame = self.frame;
            frame.size = newValue;
            self.frame = frame;
        }
    }
    
    
    //出场动画
    public func animateIn()->(){
        self.transform =  CGAffineTransform(scaleX: 0.1, y: 0.1)
        
        UIView.animate(withDuration: 0.2, animations: {
            
            self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
        }, completion: { (true) in
            
            UIView.animate(withDuration: 1/15, animations: {
                self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
            }, completion: { (true) in
                UIView.animate(withDuration: 1/7.5, animations: {
                    self.transform = CGAffineTransform(scaleX: 1, y: 1)
                }, completion: { (true) in
                    
                })
            })
        })
        
    }
    
    //回收动画
    public func animateOut()->(){
        
        UIView.animate(withDuration: 0.2, animations: {
            self.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
        }, completion: { (true) in
            
            UIView.animate(withDuration: 1/15, animations: {
                self.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
            }, completion: { (true) in
                UIView.animate(withDuration: 1/7.5, animations: {
                    self.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                }, completion: { (true) in
                    self.removeFromSuperview()
                })
            })
        })
        
    }
    
    
    ///给按钮设置菊花展示
    public func addActivityOnBtn(){
        self .addActivityOnBtn(color: UIColor.white, scale: 0.85)
    }
    
    ///给按钮设置菊花展示 保留背景图
    public func addActivityImgOnBtn(){
        self .addActivityImgOnBtn(color: UIColor.white, scale: 0.85)
    }
    
    ///给按钮设置菊花展示
    public func addActivityOnBtn(color:UIColor, scale:CGFloat){
        
        var button:UIButton?
        
        if(self.isKind(of: UIButton.classForCoder())){
            button = (self as! UIButton)
            button?.setTitle("", for: UIControl.State.normal)
            button?.setImage(UIImage.init(named: ""), for: UIControl.State.normal)
            button?.setImage(UIImage.init(named: ""), for: UIControl.State.selected)
            button?.setBackgroundImage(UIImage.init(named: ""), for: UIControl.State.normal)
        }
        
        let addFriendActivityIndicator = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.white)
        addFriendActivityIndicator.frame = button!.bounds
        addFriendActivityIndicator.isUserInteractionEnabled = true
        addFriendActivityIndicator.color = color
        
        button?.addSubview(addFriendActivityIndicator)
        addFriendActivityIndicator .startAnimating()
        addFriendActivityIndicator.transform = CGAffineTransform.init(scaleX: scale, y: scale)
    }
    
    ///给按钮设置菊花展示
    public func addActivityImgOnBtn(color:UIColor, scale:CGFloat){
        
        var button:UIButton?
        
        if(self.isKind(of: UIButton.classForCoder())){
            button = (self as! UIButton)
            button?.setTitle("", for: UIControl.State.normal)
            button?.setImage(UIImage.init(named: ""), for: UIControl.State.normal)
            button?.setImage(UIImage.init(named: ""), for: UIControl.State.selected)
        }
        
        let addFriendActivityIndicator = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.white)
        addFriendActivityIndicator.frame = button!.bounds
        addFriendActivityIndicator.isUserInteractionEnabled = true
        addFriendActivityIndicator.color = color
        
        button?.addSubview(addFriendActivityIndicator)
        addFriendActivityIndicator .startAnimating()
        addFriendActivityIndicator.transform = CGAffineTransform.init(scaleX: scale, y: scale)
    }
    
    //按钮菊花停止展示 并显示文字
    public func closeActivityByBtn(title:NSString){
        if(self.isKind(of: UIButton.classForCoder())){
            
            let button:UIButton = (self as! UIButton)
            button.titleLabel?.isHidden = false
            button.setTitle(title as String, for: UIControl.State.normal)
            
        }
        
        var act:UIActivityIndicatorView?
        
        for view:UIView in self.subviews{
            
            if(view .isKind(of: UIActivityIndicatorView.classForCoder())){
                
                act =  (view as! UIActivityIndicatorView)
                act!.stopAnimating()
                act!.removeFromSuperview()
                act = nil
            }
        }
    }
    
    //按钮菊花停止展示 并显示图片
    public func closeActivityWithBtn(imgString:NSString){
        
        if(self.isKind(of: UIButton.classForCoder())){
            
            let button:UIButton = (self as! UIButton)
            button.titleLabel?.isHidden = false
            button.setImage(UIImage.init(named: imgString as String), for: UIControl.State.normal)
            
        }
        
        var act:UIActivityIndicatorView?
        
        for view:UIView in self.subviews{
            
            if(view .isKind(of: UIActivityIndicatorView.classForCoder())){
                
                act =  (view as! UIActivityIndicatorView)
                act!.stopAnimating()
                act!.removeFromSuperview()
                act = nil
            }
        }
    }
    
    //按钮菊花停止展示 并显示背景图片
    public func closeActivityWithBackImgBtn(imgString:NSString){
        
        if(self.isKind(of: UIButton.classForCoder())){
            
            let button:UIButton = (self as! UIButton)
            button.titleLabel?.isHidden = false
            button.setBackgroundImage(UIImage.init(named: imgString as String), for: UIControl.State.normal)
            
        }
        
        var act:UIActivityIndicatorView?
        
        for view:UIView in self.subviews{
            
            if(view .isKind(of: UIActivityIndicatorView.classForCoder())){
                
                act =  (view as! UIActivityIndicatorView)
                act!.stopAnimating()
                act!.removeFromSuperview()
                act = nil
            }
        }
    }
    
    //按钮菊花停止展示 并显示文字加图片
    public func closeActivityByImgTitleBtn(img:UIImage, title:NSString){
        if(self.isKind(of: UIButton.classForCoder())){
            
            let button:UIButton = (self as! UIButton)
            button.titleLabel?.isHidden = false
            button.setTitle(title as String, for: UIControl.State.normal)
             button.setBackgroundImage(img, for: UIControl.State.normal)
        }
        
        var act:UIActivityIndicatorView?
        
        for view:UIView in self.subviews{
            
            if(view .isKind(of: UIActivityIndicatorView.classForCoder())){
                
                act =  (view as! UIActivityIndicatorView)
                act!.stopAnimating()
                act!.removeFromSuperview()
                act = nil
            }
        }
    }
    
    //按钮菊花停止展示 并显示默认图和选中图
    public func closeActivityWithBtn(imgString:NSString, imgString2:NSString){
        
        if(self.isKind(of: UIButton.classForCoder())){
            
            let button:UIButton = (self as! UIButton)
            button.titleLabel?.isHidden = false
            button.setImage(UIImage.init(named: imgString as String), for: UIControl.State.normal)
            button.setImage(UIImage.init(named: imgString2 as String), for: UIControl.State.selected)
            
        }
        
        var act:UIActivityIndicatorView?
        
        for view:UIView in self.subviews{
            
            if(view .isKind(of: UIActivityIndicatorView.classForCoder())){
                
                act =  (view as! UIActivityIndicatorView)
                act!.stopAnimating()
                act!.removeFromSuperview()
                act = nil
            }
        }
    }
    
}
