//
//  MyAddressVC.swift
//  
//
//  Created by Vijith on 07/04/22.
//

import UIKit

class MyAddressVC: UIViewController {

    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var tableHeight: NSLayoutConstraint!
    @IBOutlet weak var currentName: UILabel!
    @IBOutlet weak var currentAddress: UILabel!
    @IBOutlet weak var deliverBtnOutlet: UIButton!
    @IBOutlet weak var deliverBtnHeight: NSLayoutConstraint!
    @IBOutlet weak var deliverBtnBottom: NSLayoutConstraint!
    
    @IBOutlet weak var topOuterView: UIView!
    var isFromCart = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

setui()
    }
    
    func setui()
    {
        
        if self.isFromCart
        {
            self.deliverBtnOutlet.isHidden = false
            self.deliverBtnHeight.constant = 50
            self.deliverBtnBottom.constant = 30
        }
        else
        {
            self.deliverBtnOutlet.isHidden = true
            self.deliverBtnHeight.constant = 0
            self.deliverBtnBottom.constant = 0
        }
        
        self.topOuterView.backgroundColor = UIColor.viewBg
    }
    override func viewWillLayoutSubviews() {
//        self.tableHeight.constant = self.table.contentSize.height
    }

    @IBAction func onTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func OnTapAddBtn(_ sender: Any) {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "AddAddressVC2")as! AddAddressVC2
        nextVc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    
    @IBAction func deleteBtnTapped(_ sender: Any) {
        let alert = UIAlertController(title: "Delete", message: "Do you want to delete the current address", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
            print("Address deleted")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alert.addAction(okAction)
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }
    
    @IBAction func editBtnTapped(_ sender: Any) {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "AddAddressVC2")as! AddAddressVC2
        nextVc.currentName = self.currentName.text ?? ""
        nextVc.currentAddress = self.currentAddress.text ?? ""
        nextVc.header = "Edit Address"
        nextVc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    
    @IBAction func OnTapDeliverBtn(_ sender: Any) {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let nextVc = sb.instantiateViewController(withIdentifier: "OrderSuccessVC")as! OrderSuccessVC
        nextVc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(nextVc, animated: true)
    }
    
}
extension MyAddressVC:UITableViewDelegate,UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let  cell = tableView.dequeueReusableCell(withIdentifier: "addresscell", for: indexPath) as! MyAddressTableViewCell
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return max(UITableView.automaticDimension, 85)
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        viewWillLayoutSubviews()
    }
    
    
}
