//
//  PutOnTheSensorViewController.swift
//  MyTatvaCare
//
//  Created by Nirav Ramani on 08/05/25.
//

import UIKit

class PutOnTheSensorViewController: UIViewController {

    @IBOutlet weak var customTopView: CustomTopUIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var bottomButton: CustomOverlayButton!
    
    enum enumTableRow: Int, CaseIterable {
        case separator
        case verticalLabel
        case video
        case importantNote
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupLayout()
    }
    
    func setupLayout() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.registerNibs([VerticalLabelsTVC.self,
                                SeparatorTVC.self,
                                VideoTVC.self,
                                NoteTVC.self])
        tableView.reloadData()
        let payload = [
            "status": "cgm_place_sensor_landing",
        ]

        NotificationCenter.default.post(
            name: Notification.Name("cgmplacesensorlanding"),
            object: nil,
            userInfo: payload
        )
        
        
        
        bottomButton.labelText = "I Have Placed The Sensor"
        bottomButton.buttonTapCallback = {
            let vc = AttachTransmitterViewController.instantiate(fromStoryboard: Enum_stroyboard.Main.rawValue)
            self.navigationController?.pushViewController(vc, animated: false)
        }

        customTopView.onDemoTapped = {
            let isForReconnect = UserDefaults.standard.bool(forKey: "isForReconnect")
            let payload = [
                "status": "cgm_watch_demo_clicked"
            ]
            
            NotificationCenter.default.post(
                name: Notification.Name("cgmwatchdemoclicked"),
                object: nil,
                userInfo: payload
            )
            let webVC = WebViewController()
            webVC.modalPresentationStyle = .formSheet
            webVC.videoURL = URL(string: isForReconnect == true ? reconnectionwatchCGMVideo : watchCGMVideo)
            self.present(webVC, animated: true, completion: nil)
        }
        
        customTopView.onCloseTapped = {
            let vc = ExitJourneyViewController.instantiate(fromStoryboard: Enum_stroyboard.Main.rawValue)
            vc.modalPresentationStyle = .overFullScreen
            self.present(vc, animated: true, completion: nil)
        }
        customTopView.onWhatsAppTapped = {
            let vc = ChatWithExpertViewController.instantiate(fromStoryboard: Enum_stroyboard.Main.rawValue)
            self.navigationController?.pushViewController(vc, animated: false)
        }
    }

}

extension PutOnTheSensorViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return enumTableRow.allCases.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch enumTableRow(rawValue: indexPath.row) {
        case .video:
            return 272
        default:
            return UITableView.automaticDimension
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch enumTableRow(rawValue: indexPath.row)! {
        case .separator:
            let cell: SeparatorTVC = tableView.dequeueReusableCell(for: indexPath)
            return cell
        case .verticalLabel:
            let cell: VerticalLabelsTVC = tableView.dequeueReusableCell(for: indexPath)
            cell.label1.text = "STEP 4"
            cell.label2.text = "Put on the sensor"
            cell.label3.text = "Watch the below video or follow the steps to put on your sensor"
            return cell
        case .video:
            let cell: VideoTVC = tableView.dequeueReusableCell(for: indexPath)
            cell.imageViewBackground.image = loadImage(named: "step4")
            cell.onPlayVideo = {
                let webVC = WebViewController()
                webVC.modalPresentationStyle = .formSheet
                webVC.videoURL = URL(string: applyingTheSensor)
                self.present(webVC, animated: true, completion: nil)
            }
            return cell
        case .importantNote:
            let cell: NoteTVC = tableView.dequeueReusableCell(for: indexPath)
            cell.setImportantNote()
//            cell.labelDesc.text = "Don’t unlock your sensor before putting it the sensor on your skin"
            cell.imageViewLeft.isHidden = true
            cell.heightConstraint.constant = 64
            return cell
        }
    }
}
