//
//  SHMineViewController.swift
//  ShorthandMaster
//
//  Created by 明津李 on 2020/8/25.
//  Copyright © 2020 明津李. All rights reserved.
//

import UIKit

class SHMineViewController: SHBaseViewController {

    @IBOutlet var tableView: UITableView!
    @IBOutlet var tableHeaderView: UIView!
    @IBOutlet var lockLab: UILabel!
    @IBOutlet var lockDetailsLab: UILabel!
    @IBOutlet var lockBtn: UIButton!
    
    var dataSource: [[String:[String]]] = [["笔记本/笔记":["排序方式"]],
                                         ["更多":["用户协议", "隐私政策", "用户反馈", "恢复购买"]]]
    
    var sortTypes = ["最新创建时间","最后修改时间"]
    var currentSortType: String =  ["最新创建时间","最后修改时间"][CRUserDefaults.sortType!]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getUserInfo()
    }
    
    override func setupUI() {
        
        self.navigationItem.title = "我的"
        
//        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "mine_setting"), style: .plain, target: self, action: #selector(setting))
        
        tableHeaderView.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 118*kAutoWidth)
        tableView.tableHeaderView = tableHeaderView
        tableView.tableFooterView = UIView.init()
        tableView.backgroundColor = UIColor.groupTableViewBackground
        tableView.layoutIfNeeded()
        tableView.layoutSubviews()
    }
    
    func getUserInfo(){
        if let userInfo = SHUserAccountManager.shared.userInfo {
            if userInfo.isMember == true{
                lockBtn.isHidden = true
                lockLab.text = "功能已解锁"
                lockDetailsLab.text = "您已是高级用户"
            }else {
                lockBtn.isHidden = false
                lockLab.text = "功能未解锁"
                lockDetailsLab.text = "解除限制，使用速记大师高级版"
            }
        }
    }
    
    @objc func setting(){
        let setting = UIStoryboard.init(name: "Mine", bundle: nil).instantiateViewController(withIdentifier: "SHSettingViewController") as! SHSettingViewController
        self.navigationController?.pushViewController(setting, animated: true)
    }
    
    func sortType(){
        UIAlertController.showActionSheet(withTitle: "", message: "", cancelBtnTitle: "取消", otherBtnTitles: sortTypes) { (index) in

            CRUserDefaults.sortType = index-1
            self.currentSortType = self.sortTypes[index-1]
            self.tableView.reloadData()
        }
    }
}

extension SHMineViewController:UITableViewDelegate, UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let dict = dataSource[section]
        return dict.values.first?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let  identifier = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: identifier)
        }
        cell?.textLabel?.text = dataSource[indexPath.section].values.first?[indexPath.row]
        cell?.textLabel?.textColor = UIColor.init(r: 34, g: 34, b: 34)
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 16)
        
        if indexPath.section == 0 {
            cell?.detailTextLabel?.text = currentSortType
            cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 12)
        }
        
        cell?.accessoryType = .disclosureIndicator
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let bgView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 44))
        bgView.backgroundColor = UIColor.clear

        let whiteView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 44))
        whiteView.backgroundColor = UIColor.white
        bgView.addSubview(whiteView)
        
        let titleLab = UILabel.init(frame: CGRect.init(x: 20, y: 19, width: 300, height: 14))
        titleLab.font = UIFont.systemFont(ofSize: 14)
        titleLab.text = dataSource[section].keys.first
        titleLab.textColor = UIColor.init(hexStr: "999999")
        whiteView.addSubview(titleLab)
        
        let lineView = UIView.init(frame: CGRect.init(x: 20, y: 44, width: SCREEN_WIDTH-20, height: 1))
        lineView.backgroundColor = UIColor.init(hexStr: "E4E9F2")
        whiteView.addSubview(lineView)
        
        if section > 0 {
            bgView.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 44+9)
            whiteView.frame = CGRect.init(x: 0, y: 9, width: SCREEN_WIDTH, height: 44)
        }
        
        return bgView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 44
        }
        return 44+9
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.section {
        case 0:
            sortType()
            break
        case 1:
            switch indexPath.row {
            case 0:
                let webView = SHWebViewController()
                webView.url = SHUserAccountManager.shared.h5_urlDic["user"] as? String
                webView.title = "用户协议"
                self.navigationController?.pushViewController(webView, animated: true)
                break
            case 1:
                let webView = SHWebViewController()
                webView.url = SHUserAccountManager.shared.h5_urlDic["privacy"] as? String
                webView.title = "隐私政策"
                self.navigationController?.pushViewController(webView, animated: true)
                break
            case 2:
                let webView = SHWebViewController()
                webView.url = SHUserAccountManager.shared.h5_urlDic["FeedBackuUrl"] as? String
                webView.title = "联系客服"
                self.navigationController?.pushViewController(webView, animated: true)
                break
            case 3:

                break
            default:
                break
            }
            
            break
        default:
            break
        }
        
//            let webView = SHWebViewController()
//            webView.url = SHUserAccountManager.shared.h5_urlDic["user"] as? String
//            webView.title = "付费用户协议"
//            self.navigationController?.pushViewController(webView, animated: true)
        
//        let delete = UIStoryboard.init(name: "Mine", bundle: nil).instantiateViewController(withIdentifier: "SHDeleteDetailsViewController") as! SHDeleteDetailsViewController
//        self.navigationController?.pushViewController(delete, animated: true)
    }
}
