//
//  SortMenuView.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/9/10.
//

import Foundation
import KLExtension
import UIKit
import Localize_Swift

enum SortType {
    case upTime /// 时间正序
    case downTime /// 时间倒序
    case upName /// 名字大小写正序
    case downName /// 名字大小写倒序
}

class SortMenuViewController: UIViewController {
    lazy var dismissArea : UIControl = {
       let control = UIControl()
        control.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return control
    }()
    lazy var bgView: UIView = .simpleView(color: .white)

    lazy var titlelabel: UILabel = .simpleLabel(text: "Sort By".localized(),font: .boldSystemFont18,color: .black)

    lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_close"), for: .normal)
        btn.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return btn
    }()

    lazy var sortbyTimeUp: SortSelectCell = .init(title: "Ascending order by time".localized(), type: .upTime)
    lazy var sortbyTimeDown: SortSelectCell = .init(title: "Descending order by time".localized(), type: .downTime)
    lazy var sortbyNameUp: SortSelectCell = .init(title: "Ascending order by name".localized(), type: .upName)
    lazy var sortbyNameDown: SortSelectCell = .init(title: "Descending order by name".localized(), type: .downName)

    var didSelectedSortType: (SortType) -> Void = { _ in }

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildren()
        makeConstraint()
    }

    convenience init(type: SortType) {
        self.init()
        switch type {
        case .upTime:
            sortbyTimeUp.isSelected = true
        case .downTime:
            sortbyTimeDown.isSelected = true
        case .upName:
            sortbyNameUp.isSelected = true
        case .downName:
            sortbyNameDown.isSelected = true
        }
    }

    @objc func dismissCurrentVc() {
        dismiss(animated: true)
    }

    func addChildren() {
        view.addSubview(dismissArea)
        view.addSubview(bgView)
        bgView.addSubview(titlelabel)
        bgView.addSubview(closeButton)
        bgView.addSubview(sortbyTimeUp)
        bgView.addSubview(sortbyTimeDown)
        bgView.addSubview(sortbyNameUp)
        bgView.addSubview(sortbyNameDown)

        sortbyTimeUp.addTarget(self, action: #selector(didSelectedSortAction(sender:)), for: .touchUpInside)
        sortbyTimeDown.addTarget(self, action: #selector(didSelectedSortAction(sender:)), for: .touchUpInside)
        sortbyNameUp.addTarget(self, action: #selector(didSelectedSortAction(sender:)), for: .touchUpInside)
        sortbyNameDown.addTarget(self, action: #selector(didSelectedSortAction(sender:)), for: .touchUpInside)
    }

    @objc func didSelectedSortAction(sender: SortSelectCell) {
        didSelectedSortType(sender.type)
        dismissCurrentVc()
    }

    func makeConstraint() {
        dismissArea.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(bgView.snp.top)
        }
        bgView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
        }

        titlelabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(30)
            make.leading.equalToSuperview().offset(16)
        }

        closeButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
            make.width.height.equalTo(30)
        }

        sortbyTimeDown.snp.makeConstraints { make in
            make.top.equalTo(titlelabel.snp.bottom).offset(14)
            make.leading.trailing.equalToSuperview()
        }
        sortbyTimeUp.snp.makeConstraints { make in
            make.top.equalTo(sortbyTimeDown.snp.bottom).offset(14)
            make.leading.trailing.equalToSuperview()
        }
        sortbyNameDown.snp.makeConstraints { make in
            make.top.equalTo(sortbyTimeUp.snp.bottom).offset(14)
            make.leading.trailing.equalToSuperview()
        }
        sortbyNameUp.snp.makeConstraints { make in
            make.top.equalTo(sortbyNameDown.snp.bottom).offset(14)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-80)
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bgView.setRectCorner(corner: [.topLeft, .topRight], radii: .init(width: 16, height: 16))
    }
}

class SortSelectCell: UIControl {
    lazy var selectIcon: UIImageView = .simpleImage(imageName: "ic_check_n")
    lazy var titleLabel: UILabel = .simpleLabel(text: "Ascending order by time".localized(),font: .systemFont16,color: .black)
        
    var type: SortType = .downName

    override var isSelected: Bool {
        didSet {
            if isSelected {
                selectIcon.image = UIImage(named: "ic_check_s")
            } else {
                selectIcon.image = UIImage(named: "ic_check_n")
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addChildren()
        makeConstraints()
    }

    convenience init(title: String, type: SortType) {
        self.init(frame: .zero)
        titleLabel.text = title
        self.type = type
    }

    func addChildren() {
        addSubview(selectIcon)
        addSubview(titleLabel)
    }

    func makeConstraints() {
        selectIcon.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.width.height.equalTo(20)
            make.verticalEdges.equalToSuperview().inset(15)
        }

        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(selectIcon.snp.trailing).offset(12)
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
