package com.ewei.demo.chat;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.baidu.mapapi.search.core.PoiInfo;
import com.ewei.demo.otherui.LocationActivity;
import com.ewei.demo.otherui.LocationShowActivity;
import com.ewei.demo.ticket.TicketActivity;
import com.ewei.sdk.chat.BaseChatActivity;
import com.ewei.sdk.constants.ChatValue;
import com.ewei.sdk.constants.CommonValue;
import com.ewei.sdk.entity.Article;
import com.ewei.sdk.entity.Attachment;
import com.ewei.sdk.entity.ChatLogMessage;
import com.ewei.sdk.entity.Ticket;
import com.ewei.sdk.other.AttachmentActivity;
import com.ewei.sdk.other.ArticleDetailWebBrowserActivity;
import com.ewei.sdk.other.PlayVideoActivity;
import com.ewei.sdk.other.SinglePreviewActivity;

/**
 * Created by Administrator on 2017/1/3.
 */

public class ChatActivity extends BaseChatActivity {

    @Override
    protected void onClickArticle(Article article) {
        ArticleDetailWebBrowserActivity.startActivity(this, article.title, article.topic.type, article.id);
    }


    @Override
    protected void onClickMap(String latitude, String longitude, String addressStr) {
//        /**
//         * 百度网页版地图展示
//         */
//        String httpUrl = "https://api.map.baidu.com/marker?location=" + latitude + "," + longitude + "&title=我的位置&content=" + addressStr + "&output=html";
//        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(httpUrl));
//        startActivity(intent);
        Intent intent = new Intent(this, LocationShowActivity.class);
        intent.putExtra(CommonValue.LOCATION_ADDRESS, addressStr);
        intent.putExtra(CommonValue.LOCATION_LONGITUDE, longitude);
        intent.putExtra(CommonValue.LOCATION_LATITUDE, latitude);
        startActivity(intent);
    }

    @Override
    protected void onClickAttachment(Attachment attachment, String localUrl, String fileType) {
        switch (fileType) {
            case ChatValue.TYPE_CHATLOG_VIDEO:
                PlayVideoActivity.startActivity(this, attachment);
                break;
            case ChatValue.TYPE_CHATLOG_IMAGE:
                SinglePreviewActivity.startActivity(this, attachment);
                break;
            default:
                AttachmentActivity.startAttachment(this, attachment);
                break;
        }
    }

    @Override
    protected void onClickTicket(int ticketId) {
        TicketActivity.startTicketActivity(this, new Ticket(ticketId), TicketActivity.class, 0);
    }

    @Override
    protected View addTextView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultTextView(chatLog, isLeft);
    }

    @Override
    protected View addFileView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultFileView(chatLog, isLeft);
    }

    @Override
    protected View addAudioView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultAudioView(chatLog, isLeft);
    }

    @Override
    protected View addVideoView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultVideoView(chatLog, isLeft);
    }

    @Override
    protected View addImageView(final ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultImageView(chatLog, isLeft);
    }

    @Override
    protected View addLocationView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultLocationView(chatLog, isLeft);
    }

    @Override
    protected View addKnowledgeView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultKnowledgeView(chatLog, isLeft);
    }

    @Override
    protected View addUnknownView(ChatLogMessage chatLog, boolean isLeft) {
        return getDefaultUnknownView(chatLog, isLeft);
    }

    @Override
    protected boolean isOpenLocation() {
        return true;
    }

    @Override
    protected boolean isOpenFileUpload() {
        return true;
    }

    @Override
    protected void clickMapLocation() {
        Intent intent = new Intent(this, LocationActivity.class);
        startActivityForResult(intent, CommonValue.REQUEST_CODE_LOCATION);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CommonValue.REQUEST_CODE_LOCATION:
                    //定位
                    Bundle bundle = data.getExtras();
                    PoiInfo pi = null;
                    if (bundle != null) {
                        pi = bundle.getParcelable("poiinfo");
                    }
                    String path = data.getStringExtra("path");
                    if (!TextUtils.isEmpty(path) && pi != null) {
                        addLocationReply(path, pi.location.latitude, pi.location.longitude, pi.address);
                    } else {
                        showToast("无法获取定位信息!");
                    }
                    break;
                default:
                    break;
            }
        }
    }
}