# coding:utf-8
import os
import re
import sys
import time

import itchat
from itchat.content import (ATTACHMENT, CARD, FRIENDS, MAP, NOTE, PICTURE, RECORDING, SHARING, TEXT, VIDEO)

sys.path.append(r'../')
import crawl.pm25 as pm25
from robot.tulin import get_response

  
 
msg_information = {}
face_bug=None #针对表情包的内容
owner = None
robot_enabled = False

def my_reply(fromMsg, toUser):
    robotReply = get_response(fromMsg)
    itchat.send(msg=robotReply, toUserName=toUser)
    print("机器人: {}".format(robotReply))

def send_pm25(city, toUser):
    if city == None or city == '':
        city = 'suzhou'
    pm25Data = pm25.getPM25(city)
    itchat.send(msg=pm25Data, toUserName=toUser)
     
 
def is_sent_me(toUserName):
    friend = itchat.search_friends(userName=toUserName)
    if friend != None and friend.Uin == owner.Uin:
        return True
    return False
  
@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isGroupChat=True, isMpChat=True)
def handle_receive_msg(msg):
    global face_bug
    msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  #接受消息的时间 
    fromName = msg['FromUserName'] 
    toUserName = msg['ToUserName']
    nickName = ''
    friend = itchat.search_friends(userName=fromName)
    mp = itchat.search_mps(userName=fromName)
    if friend != None :
        if(friend.RemarkName != ''):
            nickName = friend.RemarkName
        else:
            nickName = friend['NickName'] #在好友列表中查询发送信息的好友昵称 
    elif mp != None: 
        nickName = mp['NickName']
    elif mp == None and friend == None and 'ActualNickName' in msg:
        nickName = msg.ActualNickName
    else:       
        nickName = fromName
    msg_from = nickName
    msg_time = msg['CreateTime']  #信息发送的时间
    msg_id = msg['MsgId']  #每条信息的id
    msg_content = None   #储存信息的内容
    msg_share_url = None  #储存分享的链接，比如分享的文章和音乐 
    #print ('from: %s, type: %s' % (msg_from,msg['Type'])) 
    wechatNickName = "{0} : ".format(msg_from)
    print(wechatNickName)

    if msg['Type'] == 'Text' or msg['Type'] == 'Friends':   #如果发送的消息是文本或者好友推荐
        msg_content = msg['Text'] 
        print(msg_content)
    
    #如果发送的消息是附件、视屏、图片、语音
    elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \
        or msg['Type'] == 'Picture' \
        or msg['Type'] == 'Recording':
        msg_content = msg['FileName']  #内容就是他们的文件名
        msg['Text'](str(msg_content))  #下载文件 
        print("发送了一段{0}. 已经下载, 请查看.".format(msg['Type']))

    elif msg['Type'] == 'Card':  #如果消息是推荐的名片
        msg_content = msg['RecommendInfo']['NickName'] + '的名片'  #内容就是推荐人的昵称和性别
        if msg['RecommendInfo']['Sex'] == 1:
            msg_content += '性别为男'
        else:
            msg_content += '性别为女'
    
        print('推荐了{0}.'.format(msg_content))

    elif msg['Type'] == 'Map':  #如果消息为分享的位置信息
        x, y, location = re.search( "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
        if location is None:
            msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()   #内容为详细的地址
        else:
            msg_content = r"" + location

        print("发布了自己的位置: {}".format(msg_content)) 


    elif msg['Type'] == 'Sharing':   #如果消息为分享的音乐或者文章，详细的内容为文章的标题或者是分享的名字
        msg_content = msg['Text']
        msg_share_url = msg['Url']    #记录分享的url
        print ("发布了一条分享:{}".format(msg_content))
        print ("链接地址: {}".format(msg_share_url))
    
    face_bug=msg_content
 
    if( is_sent_me(toUserName) and robot_enabled and friend):       #如果是发送给我的, 用机器人回复.
        my_reply(msg_content, fromName)
    
    if(toUserName == 'filehelper'):
        msg_content = msg['Text']
        mh = re.search(r'pm:(\w*)', msg_content)
        if(mh) :
            city = mh.group(1)
            send_pm25(city, toUserName)
     
    ##将信息存储在字典中，每一个msg_id对应一条信息
    msg_information.update(
        {
        msg_id: {
            "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
            "msg_type": msg["Type"],
            "msg_content": msg_content, "msg_share_url": msg_share_url
        }
        }
    )
 
#@itchat.msg_register(TEXT)
def pm25_listener(msg):
    print(msg)
    toUserName = msg['ToUserName']
    if(toUserName == 'filehelper'):
        msg_content = msg['Text']
        mh = re.search(r'pm:(\w*)', msg_content)
        if(mh) :
            city = mh.group(1)
            send_pm25(city, toUserName)


##这个是用于监听是否有消息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def information(msg):
    #这里如果这里的msg['Content']中包含消息撤回和id，就执行下面的语句
    if '撤回了一条消息' in msg['Content']:
        old_msg_id = re.search(r"\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)  #在返回的content查找撤回的消息的id
        old_msg = msg_information.get(old_msg_id)  #得到消息
        #print (old_msg)

    if len(old_msg_id)<11: #如果发送的是表情包
        itchat.send_file(face_bug,toUserName='filehelper')
    else: #发送撤回的提示给文件助手
        msg_body = "告诉你一个秘密~" + "\n" \
            + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \
            + old_msg.get('msg_time_rec') + "\n" \
            + "撤回了什么 ⇣" + "\n" \
            + r"" + old_msg.get('msg_content')
      #如果是分享的文件被撤回了，那么就将分享的url加在msg_body中发送给文件助手
    if old_msg['msg_type'] == "Sharing":
        msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url')
 
    # 将撤回消息发送到文件助手
    itchat.send_msg(msg_body, toUserName='filehelper')
    # 有文件的话也要将文件发送回去
    if old_msg["msg_type"] == "Picture" \
          or old_msg["msg_type"] == "Recording" \
          or old_msg["msg_type"] == "Video" \
          or old_msg["msg_type"] == "Attachment":
        file = '@fil@%s' % (old_msg['msg_content'])
        itchat.send(msg=file, toUserName='filehelper')
        os.remove(old_msg['msg_content'])
    
    # 删除字典旧消息
    msg_information.pop(old_msg_id)
 
def main():
    #itchat.auto_login(hotReload=True, enableCmdQR=2, statusStorageDir='account.pkl')  #通过命令行打印登入二维码
    global robot_enabled, owner
    robot_enabled = len(sys.argv) > 1 and sys.argv[1]
    itchat.auto_login(hotReload=True)
    owner = itchat.get_friends()[0]
    itchat.run()

if __name__ == "__main__": 
    main()
