﻿import UIKit
let xv = CGFloat(UIScreen.main.bounds.size.width)
let yv = CGFloat(UIScreen.main.bounds.size.height)
var textSet = ["AAAAA,aaaa,11111,a1_a2_a3_a4_a5_a6_a7_a8_a9","BBBBB,bbbbb,22222,b1_b2_b3_b4_b5_b6_b7_b8_b9","CCCCC,ccccc,33333,c1_c2_c3_c4_c5_c6_c7_c8_c9"]
var showOpen = false
let showView = UIView(frame: CGRect(x: 0, y: 0, width: xv, height: yv))
class ViewController: UIViewController,UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: xv*0.8, height: 90)
        layout.minimumLineSpacing = 25
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.delegate   = self as UICollectionViewDelegate
        collectionView.dataSource = self as UICollectionViewDataSource
        collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        self.view.addSubview(collectionView)
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        collectionView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        collectionView.backgroundColor = .lightGray
        
        showView.alpha = 0.0
        self.view.addSubview(showView)
        
        let showButton = UIButton(frame: CGRect(x: 0, y: 0, width: xv, height: yv))
        showButton.addTarget(self, action: #selector(showTapped), for: .touchUpInside)
        showButton.backgroundColor = .black
        
        showView.addSubview(showButton)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return textSet.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
        let arr = textSet[indexPath.row].components(separatedBy: ",")
        cell.genreLabel.text = arr[1]
        cell.timesLabel.text = arr[2]
        cell.titleLabel.text = arr[0]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        print("showViewにtextSet[\(indexPath.row)][3]を画像のように表示")
        showTapped()
    }


    @objc func showTapped(){
        if(showOpen){
            showView.alpha = 0.0
            showOpen = false
        }else{
            showView.alpha = 1.0
            showOpen = true
        }
    }
}
class MyCollectionViewCell: UICollectionViewCell {
var genreLabel = UILabel()
var timesLabel = UILabel()
var titleLabel = UILabel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.addSubview(genreLabel)
        self.contentView.addSubview(timesLabel)
        self.contentView.addSubview(titleLabel)
        genreLabel.translatesAutoresizingMaskIntoConstraints = false
        genreLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        genreLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
        genreLabel.widthAnchor.constraint(equalToConstant:xv*0.8-20).isActive = true
        timesLabel.translatesAutoresizingMaskIntoConstraints = false
        timesLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        timesLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 8).isActive = true
        timesLabel.widthAnchor.constraint(equalToConstant:xv*0.8-20).isActive = true
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10).isActive = true
        titleLabel.widthAnchor.constraint(equalToConstant:xv*0.8-20).isActive = true


        self.contentView.backgroundColor = .white
        genreLabel.textColor = .systemPink
        genreLabel.font = UIFont.systemFont(ofSize: 14)
        timesLabel.textAlignment = .right
        timesLabel.textColor = .black
        timesLabel.font = UIFont.systemFont(ofSize: 14)
        titleLabel.textColor = .black
        titleLabel.font = UIFont.systemFont(ofSize: 36)
    }
    required init?(coder: NSCoder) {
        fatalError("An error occured.")
    }
}