//
//  ContactPersonInfoV2ViewController.swift
//  O2Platform
//
//  Created by 程剑 on 2017/7/11.
//  Copyright © 2017年 zoneland. All rights reserved.
//

import UIKit
import Alamofire
import AlamofireImage
import AlamofireObjectMapper
import SwiftyJSON
import ObjectMapper

import Eureka
import CocoaLumberjack


class ContactPersonInfoV2ViewController: UITableViewController {
    @IBOutlet weak var beijingImg: UIImageView!
    @IBOutlet weak var personImg: UIImageView!
    @IBOutlet weak var personName: UILabel!
    @IBOutlet weak var personQQ: UILabel!
    @IBOutlet weak var personCollect: UIButton!
    
    @IBOutlet weak var personGirl: UIButton!
    @IBOutlet weak var personMan: UIButton!
    @IBOutlet weak var personCollectLab: UILabel!
    
    var isCollect = false
    @IBAction func collectPerson(_ sender: UIButton) {
        
        let me = O2AuthSDK.shared.myInfo()
        if personCollect.isSelected == true {
            //删除
            DBManager.shared.deleteContactData(contact!, (me?.id)!)
        }else{
            //增加
            DBManager.shared.insertContactData(contact!, (me?.id)!)
        }
        personCollect.isSelected = !personCollect.isSelected
        
    }
    
    let nameLabs = ["企业信息","姓名","员工号","唯一编码","联系电话","电子邮件","部门"]
    
    var myPersonURL:String?
    
    
    var identity:IdentityV2? {
        didSet {
            self.myPersonURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personInfoByNameQuery, parameter: ["##name##":identity!.person! as AnyObject])
        }
    }
    
    var person:PersonV2?{
        didSet {
            self.myPersonURL = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personInfoByNameQuery, parameter: ["##name##":person!.id! as AnyObject])
        }
    }
    
    var isLoadPerson:Bool = true
    
    var contact:PersonV2? {
        didSet {
            isLoadPerson = false
        }
    }
    //im 聊天
    private lazy var viewModel: IMViewModel = {
        return IMViewModel()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.beijingImg.theme_image = ThemeImagePicker(keyPath: "Icon.pic_beijing1")
        self.personImg.layer.cornerRadius = self.personImg.frame.size.width / 2
        self.personImg.clipsToBounds = true
        loadPersonInfo(nil)
        let startChatButton = OOBaseUIButton(x: (kScreenW - 260)/2, y: 5, w: 260, h: 30, target: self, action: #selector(_startChat))
        startChatButton.setTitle("发起聊天", for: .normal)
        let btnContainerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 40))
        btnContainerView.addSubview(startChatButton)
        tableView.tableFooterView = btnContainerView
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //去掉nav底部分割线
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc private func _startChat() {
        var username = ""
        if self.contact != nil {
            username = self.contact?.distinguishedName ?? ""
        }else if self.person != nil {
            username = self.person?.distinguishedName ?? ""
        }
        if username == "" {
            self.showError(title: "无法创建聊天！")
            return
        }
        
        self.viewModel.createConversation(type: o2_im_conversation_type_single, users: [username]).then { (conv) in
            let chatView = IMChatViewController()
            chatView.conversation = conv
            self.navigationController?.pushViewController(chatView, animated: true)
        }.catch { (err) in
            self.showError(title: "创建单聊失败, \(err.localizedDescription)")
        }
        
        
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return nameLabs.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "personInfoCell", for: indexPath) as! ContactPersonInfoCell

        cell.nameLab.text = self.nameLabs[indexPath.row]
        switch self.nameLabs[indexPath.row] {
        case "企业信息":
            cell.nameLab.font = UIFont.systemFont(ofSize: 17)
            cell.nameLab.textColor = UIColor.black
            cell.valueLab.isHidden = true
            cell.eventBut.isHidden = true
        case "姓名":
            cell.valueLab.text = self.contact?.name
            cell.eventBut.isHidden = true
        case "员工号":
            cell.valueLab.text = self.contact?.employee
            cell.eventBut.isHidden = true
        case "唯一编码":
            cell.valueLab.text = self.contact?.unique
            cell.eventBut.isHidden = true
        case "联系电话":
            cell.valueLab.text = self.contact?.mobile
            cell.eventBut.addTarget(self, action: #selector(self.call), for: UIControl.Event.touchUpInside)
        case "电子邮件":
            cell.valueLab.text = self.contact?.mail
            cell.eventBut.theme_setImage(ThemeImagePicker(keyPath:"Icon.icon_email"), forState: .normal)
            cell.eventBut.addTarget(self, action: #selector(self.sendMail), for: UIControl.Event.touchUpInside)
        case "部门":
            var unitName = ""
            if let idenList = self.contact?.woIdentityList {
                for iden in idenList {
                    if let unit = iden.woUnit {
                        if unitName != "" {
                            unitName.append(";")
                        }
                        unitName.append(unit.name ?? "")
                    }
                }
            }
            cell.valueLab.text = unitName
            cell.eventBut.isHidden = true
        default:
            break
        }
        

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch self.nameLabs[indexPath.row] {
        case "联系电话":
            self.call()
        case "电子邮件":
            self.sendMail()
        default:
            break
        }
        self.tableView.deselectRow(at: indexPath, animated: true)
    }
    
    

    @objc func sendMail() {
        if let mail = self.contact?.mail, mail != "" {
            let alertController = UIAlertController(title: "", message: nil,preferredStyle: .actionSheet)
            let mailAction = UIAlertAction(title: "发邮件", style: .default, handler: { _ in
                let mailURL = URL(string: "mailto://\(mail)")
                
                if UIApplication.shared.canOpenURL(mailURL!) {
                    UIApplication.shared.openURL(mailURL!)
                }else{
                    self.showError(title: "发邮件失败")
                }
            })
            let copyAction = UIAlertAction(title: "复制", style: .default, handler: { _ in
                UIPasteboard.general.string = mail
                self.showSuccess(title: "复制成功")
            })
            
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            alertController.addAction(mailAction)
            alertController.addAction(copyAction)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    
    @objc func call(){
        if let phone = self.contact?.mobile, phone != "" {
            let alertController = UIAlertController(title: "", message: nil,preferredStyle: .actionSheet)
            let smsAction = UIAlertAction(title: "发短信", style: .default, handler: { _ in
                let smsURL = URL(string: "sms://\(phone)")
                if UIApplication.shared.canOpenURL(smsURL!) {
                    UIApplication.shared.openURL(smsURL!)
                }else{
                    self.showError(title: "发短信失败")
                }
            })
            let phoneAction = UIAlertAction(title: "打电话", style: .default, handler: { _ in
                let phoneURL = URL(string: "tel://\(phone)")
                if UIApplication.shared.canOpenURL(phoneURL!) {
                    UIApplication.shared.openURL(phoneURL!)
                }else{
                    self.showError(title: "打电话失败")
                }
            })
            let copyAction = UIAlertAction(title: "复制", style: .default, handler: { _ in
                UIPasteboard.general.string = phone
                self.showSuccess(title: "复制成功")
            })
            
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            alertController.addAction(phoneAction)
            alertController.addAction(smsAction)
            alertController.addAction(copyAction)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    
    func loadPersonInfo(_ sender: AnyObject?){
        self.showLoading(title: "加载中...")
        AF.request(myPersonURL!).responseJSON {
            response in
            switch response.result {
            case .success( let val):
                let json = JSON(val)["data"]
                self.contact = Mapper<PersonV2>().map(JSONString:json.description)!
                //OOCon
                let me = O2AuthSDK.shared.myInfo()
                self.isCollect = DBManager.shared.isCollect(self.contact!, (me?.id)!)
                if self.isCollect == true {
                    self.personCollect.isSelected = true
                }else{
                    self.personCollect.isSelected = false
                }
                self.personName.text = self.contact?.name
                if let qq = self.contact?.qq, qq != "" {
                    self.personQQ.text = "QQ \(qq)"
                }else{
                    self.personQQ.text = ""
                }
                if let gt = self.contact?.genderType, gt == "f" {
                    self.personGirl.setImage(UIImage(named: "icon_girl_2"), for: .normal)
                }else{
                    self.personMan.setImage(UIImage(named: "icon_boy_2"), for: .normal)
                }
                let urlstr = AppDelegate.o2Collect.generateURLWithAppContextKey(ContactContext.contactsContextKeyV2, query: ContactContext.personIconByNameQueryV2, parameter: ["##name##":self.contact?.unique as AnyObject], generateTime: false)
                let url = URL(string: urlstr!)
                self.personImg.hnk_setImageFromURL(url!)
                DispatchQueue.main.async {
                    self.hideLoading()
                    self.tableView.reloadData()
                }
            case .failure(let err):
                DDLogError(err.localizedDescription)
                DispatchQueue.main.async {
                    self.showError(title: "加载失败")
                }
            }
            
        }
    }
}
