//
//  AV_SportsVC.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/7/3.
//

import UIKit

class AV_SportsVC: AV_RootVC {
    @IBOutlet weak var tableView: UITableView!
    
    
    var headarr = ["Exerciseregularly","Occasionalexercise","Almostnoexercise"]
    var namearr = ["Exercise regularly","Occasional exercise","Almost no exercise"]
    
    var selectedIndex: IndexPath?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setTableViewcell()
        requestTrackingAuthorization { idfa in
            if let idfa = idfa {
                print("IDFA: \(idfa)")
                StartManager.shared.idfaid = idfa
                print("Stored IDFA: \(StartManager.shared.idfaid ?? "N/A")")
            } else {
                print("IDFA is not available or tracking authorization denied.")
            }
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
        
    }
    func setTableViewcell(){
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "AV_SportsCell", bundle: nil), forCellReuseIdentifier: "AV_SportsCell")
        
    }
    
    @IBAction func backBtn(_ sender: UIButton) {
        navigationController?.popViewController(animated: true)
    }
    
    @IBAction func skip(_ sender: Any) {
        let defaults = UserDefaults.standard
        defaults.set(2000, forKey: "waterml")
        let appDelegate = UIApplication.shared.delegate
        appDelegate?.window??.rootViewController = AV_NAVVC(rootViewController: AV_WaterHomeVC())

    }
    
    @IBAction func next(_ sender: Any) {
        let vc = AV_TemperatureVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}
extension AV_SportsVC:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
            return 3
    
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "AV_SportsCell", for: indexPath) as! AV_SportsCell

            cell.headImagV.image = UIImage(named: self.headarr[indexPath.row])
            cell.nameLabel.text = self.namearr[indexPath.row]
            
            if indexPath == selectedIndex {
           
                cell.animateGradient(to: [UIColor.hexSting(color: "#69CCFB", alpha: 1).cgColor, //
                                          UIColor.hexSting(color: "#69CCFB", alpha: 1).cgColor])
            } else {
          
                cell.animateGradient(to: [UIColor.hexSting(color: "#F7F8F9", alpha: 1).cgColor, UIColor.hexSting(color: "F7F8F9", alpha: 1).cgColor])
            }
            
            return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            let previousSelectedIndex = selectedIndex
            
            // 更新选定的索引
            if selectedIndex == indexPath {
                selectedIndex = nil // 如果再次点击同一行则取消选择
            } else {
                selectedIndex = indexPath
            }
            
            //  重新加载先前选定的单元格和当前选定的单元格
            var indexPathsToReload: [IndexPath] = [indexPath]
            if let previous = previousSelectedIndex {
                indexPathsToReload.append(previous)
            }
            
            tableView.reloadRows(at: indexPathsToReload, with: .automatic)
        }
        
        if indexPath.row == 0{
            let defaults = UserDefaults.standard
            defaults.set("Exercise regularly", forKey: "sports")
        }else if indexPath.row == 1{
            let defaults = UserDefaults.standard
            defaults.set("Occasional exercise", forKey: "sports")
        }else if indexPath.row == 2{
            let defaults = UserDefaults.standard
            defaults.set("Almost no exercise", forKey: "sports")
        }
        
    }
    
}
