//
//  ARKitViewRepresentable.swift
//  DouShouQi_App
//
//  Created by etudiant on 24/06/2024.
//

import SwiftUI
import ARKit
import RealityKit
import DouShouQiModel

class ARKitViewRepresentable: ARView {
    
    required init(frame frameRect: CGRect) {
        super.init(frame: frameRect)
    }
    
    @MainActor required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) not implemented")
    }
    
    convenience init(imageBoard: String = "board", cell_size: Float = 0.1145) {
        self.init(frame: UIScreen.main.bounds)
        do {
            let game = try Game(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: "Player 1", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Player 2", andId: .player2)!)
            initBoard(withBoard: game.board, andImage: imageBoard)
        } catch {
            print("error")
        }
    }
    
    func applyConfiguration() {
        let conf = ARWorldTrackingConfiguration()
        self.session.run(conf)
    }
    
    func initBoard(withBoard board: Board, andImage image: String) {
        let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(1, 1)))
        let arBoard = ARBoard(image: image)
        anchor.addChild(arBoard)
        self.scene.addAnchor(anchor)
    }
}
