//
//  FaceShotBottomView.swift
//  Gengmei
//
//  Created by Locus on 2019/8/13.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

import UIKit

class FaceShotBottomCell: GMCollectionViewCell {
    let titleLabel = UILabel()
    let dotView = UIView()
    
    var selectedCell: Bool = false {
        didSet {
            if selectedCell {
                titleLabel.font = UIFont.gmBoldFont(16)
                dotView.isHidden = false
            } else {
                titleLabel.font = UIFont.gmFont(14)
                dotView.isHidden = true;
            }
        }
    }
    
    override func setup() {
        titleLabel.font = UIFont.gmFont(13)
        titleLabel.textColor = UIColor.white
        addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(-12);
            make.centerX.equalToSuperview()
        }
        
        dotView.backgroundColor = UIColor.white
        dotView.isHidden = true;
        dotView.layer.cornerRadius = 2
        dotView.layer.masksToBounds = true;
        addSubview(dotView)
        
        dotView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(4)
        }
    }
}

@objc protocol FaceShotBottomViewDelegate {
    func faceShotBottomCellDidSelected(type : GMScanAnimationType)
}

class FaceShotBottomView: UIView, UICollectionViewDataSource, UICollectionViewDelegate {

    weak var delegate: FaceShotBottomViewDelegate?
    var animationType: GMScanAnimationType = .faceApp
    var collectionView: UICollectionView?
    
    var selectedCell: FaceShotBottomCell? {
        willSet {
            if (newValue != nil) && (newValue!.selectedCell == false)  {
                guard let index = collectionView?.indexPath(for: newValue!)?.row else {
                    return
                }
                animationType = GMScanAnimationType(rawValue: index) ?? .faceApp
                selectedCell?.selectedCell = false
                newValue!.selectedCell = true
                UIView.animate(withDuration: 0.2) {
                    self.mj_x = (UIScreen.main.bounds.size.width - 180) / 2.0 - CGFloat((index - 1) * 60);
                }
                // 添加代理方法
                if delegate != nil {
                    print(self.animationType)
                    delegate?.faceShotBottomCellDidSelected(type: self.animationType)
                    
                }
            }
        }
    }
    
    override init(frame: CGRect) {
       super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     func setupUI() {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: 60, height: 30)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView.init(frame: CGRect(x:0, y:0, width:self.width, height:self.height), collectionViewLayout: layout)
        collectionView?.backgroundColor = UIColor.clear
        collectionView?.delegate = self
        collectionView?.dataSource = self
        addSubview(collectionView!)
        collectionView?.register(cell: FaceShotBottomCell.classForCoder())
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:FaceShotBottomCell = collectionView.dequeue(cell: FaceShotBottomCell.classForCoder(), for: indexPath) as! FaceShotBottomCell
        if indexPath.row == 0 {
            cell.titleLabel.text = "人脸拟合"
            cell.selectedCell = true
            selectedCell = cell
        } else if indexPath.row == 1 {
            cell.titleLabel.text = "AI测脸"
            cell.selectedCell = false
        } else if indexPath.row == 2 {
            cell.titleLabel.text = "AI测肤"
            cell.selectedCell = false
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedCell = collectionView.cellForItem(at: indexPath) as? FaceShotBottomCell
    }
}
