//
//  LNProfileTabView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/30.
//

import Foundation
import UIKit
import SnapKit


enum LNProfileTabType {
    case info
    case photo
}


protocol LNProfileTabViewDelegate: AnyObject {
    func onProfileTabView(view: LNProfileTabView, didSelect at: Int, type: LNProfileTabType)
}


class LNProfileTabView: UIView {
    private let indicator = UIImageView()
    private let infoTab = UIButton()
    private let photoTab = UIButton()
    
    weak var delegate: LNProfileTabViewDelegate?
    
    var curSelected: LNProfileTabType = .info {
        didSet {
            let index: Int
            switch curSelected {
            case .info:
                indicator.snp.remakeConstraints { make in
                    make.horizontalEdges.equalTo(infoTab)
                    make.bottom.equalTo(infoTab)
                    make.height.equalTo(2)
                }
                infoTab.isSelected = true
                photoTab.isSelected = false
                index = 0
            case .photo:
                indicator.snp.remakeConstraints { make in
                    make.horizontalEdges.equalTo(photoTab)
                    make.bottom.equalTo(photoTab)
                    make.height.equalTo(2)
                }
                infoTab.isSelected = false
                photoTab.isSelected = true
                index = 1
            }
            
            if oldValue != curSelected {
                UIView.animate(withDuration: 0.25) { [weak self] in
                    guard let self else { return }
                    self.layoutIfNeeded()
                }
                delegate?.onProfileTabView(view: self, didSelect: index, type: curSelected)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileTabView {
    private func setupViews() {
        snp.makeConstraints { make in
            make.height.equalTo(52)
        }
        
        infoTab.setTitleColor(.text_5, for: .selected)
        infoTab.setTitleColor(.text_3, for: .normal)
        infoTab.setTitle(.init(key: "A00230"), for: .normal)
        infoTab.titleLabel?.font = .heading_h4
        infoTab.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            self.curSelected = .info
        }), for: .touchUpInside)
        addSubview(infoTab)
        infoTab.snp.makeConstraints { make in
            make.leading.verticalEdges.equalToSuperview()
        }
        
        photoTab.setTitleColor(.text_5, for: .selected)
        photoTab.setTitleColor(.text_3, for: .normal)
        photoTab.setTitle(.init(key: "A00231"), for: .normal)
        photoTab.titleLabel?.font = .heading_h4
        photoTab.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            self.curSelected = .photo
        }), for: .touchUpInside)
        addSubview(photoTab)
        photoTab.snp.makeConstraints { make in
            make.trailing.verticalEdges.equalToSuperview()
            make.leading.equalTo(infoTab.snp.trailing)
            make.width.equalTo(infoTab)
        }
        
        let line = UIView()
        line.backgroundColor = .fill_2
        addSubview(line)
        line.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        indicator.image = .primary_7
        addSubview(indicator)
    }
}
