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

import UIKit

class AV_SelectGenderVC: AV_RootVC {
    
    @IBOutlet weak var tableView: UITableView!
    
    var headarr = ["Man","Woman","Pregnant","Lactation","Other"]
    var namearr = ["Man","Woman","Pregnant","Lactation","Other"]
    
    var selectedIndex: IndexPath?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setTableViewcell()
//        ["日", "一", "二", "三", "四", "五", "六"]
        let defaults = UserDefaults.standard
        let weekdays = ["日", "一", "二", "三", "四", "五", "六"]
        for day in weekdays {
            defaults.set(0, forKey: day)
        }

        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_SelectGenderCell", bundle: nil), forCellReuseIdentifier: "AV_SelectGenderCell")
        
    }
    
    @IBAction func skipBtn(_ sender: Any) {
        let appDelegate = UIApplication.shared.delegate
        appDelegate?.window??.rootViewController = AV_NAVVC(rootViewController: AV_WaterHomeVC())
        let defaults = UserDefaults.standard
        defaults.set(2000, forKey: "waterml")
        
        if defaults.object(forKey: "waveView") != nil {
            print("键 'answer' 存在")
        } else {
            print("键 'answer' 不存在")
            defaults.set(0, forKey: "waveView")
        }
    }
    
    
    @IBAction func nextBtn(_ sender: Any) {
        let vc = AV_selectWeightVC()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}
extension AV_SelectGenderVC:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 5
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AV_SelectGenderCell", for: indexPath) as! AV_SelectGenderCell
        
        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) {
        
        
        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("Man", forKey: "people")
        }else if indexPath.row == 1{
            let defaults = UserDefaults.standard
            defaults.set("Woman", forKey: "people")
        }else if indexPath.row == 2{
            let defaults = UserDefaults.standard
            defaults.set("Pregnancy", forKey: "people")
        }else if indexPath.row == 3{
            let defaults = UserDefaults.standard
            defaults.set("Lactation", forKey: "people")
        }
        
    }
    
}
