package com.ewei.demo.ticket;

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

import com.baidu.mapapi.search.core.PoiInfo;
import com.ewei.demo.R;
import com.ewei.demo.chat.ChatActivity;
import com.ewei.demo.chat.ChatLogActivity;
import com.ewei.demo.otherui.LocationActivity;
import com.ewei.demo.otherui.LocationShowActivity;
import com.ewei.sdk.chat.ChatUtils;
import com.ewei.sdk.constants.CommonValue;
import com.ewei.sdk.constants.TicketValue;
import com.ewei.sdk.entity.Article;
import com.ewei.sdk.entity.Attachment;
import com.ewei.sdk.entity.Ticket;
import com.ewei.sdk.other.AttachmentActivity;
import com.ewei.sdk.ticket.BaseTicketActivity;
import com.ewei.sdk.utility.Utils;
import com.ewei.sdk.other.ArticleDetailWebBrowserActivity;
import com.ewei.sdk.other.PlayVideoActivity;
import com.ewei.sdk.other.SinglePreviewActivity;

/**
 * 工单详情
 *
 * @author Jay
 * @date 2016/7/22.
 */
public class TicketActivity extends BaseTicketActivity {

    private TextView mTvTextTitle;
    private TextView mTvStatus;
    private TextView mTvHandler;


    @Override
    protected void initHeadView(View mHeadView) {
        mTvTextTitle = (TextView) mHeadView.findViewById(R.id.tv_ticket_subject);
        mTvStatus = (TextView) mHeadView.findViewById(R.id.tv_ticket_status);
        mTvHandler = (TextView) mHeadView.findViewById(R.id.tv_ticket_detail_handler);
    }

    @Override
    protected Integer getHeadViewLayout() {
        return R.layout.include_ticket_head_view;
    }


    @Override
    protected void refreshHeadControl(Ticket mTicket) {
        if (mTicket == null) {
            return;
        }

        if (!TextUtils.isEmpty(mTicket.subject)) {
            mTvTextTitle.setText(mTicket.subject);
        }

        if (!TextUtils.isEmpty(mTicket.getStatus())) {
            switch (mTicket.getStatus()) {
                case TicketValue.TICKET_STATUS_NEW:
                    //未分派的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_new);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("未分派");
                    break;
                case TicketValue.TICKET_STATUS_OPEN:
                    //进行中的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_open);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("进行中");
                    break;
                case TicketValue.TICKET_STATUS_SOLVED:
                    //处理完毕的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_solved);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("处理完毕");
                    break;
                case TicketValue.TICKET_STATUS_CLOSED:
                    //已关闭的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_closed);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("已关闭");
                    break;
                case TicketValue.TICKET_STATUS_PENDING:
                    //暂停的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_pending);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("已暂停");
                    break;
                case TicketValue.TICKET_STATUS_DELETED:
                    //回收站的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_deleted);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.et_text_color));
                    mTvStatus.setText("已删除");
                    break;
                case TicketValue.TICKET_STATUS_SUSPENDED:
                    //隔离区的
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_suspend);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("隔离区");
                    break;
                case TicketValue.TICKET_STATUS_ASSIGN:
                    //已分派
                    mTvStatus.setBackgroundResource(R.drawable.shape_ticket_status_assigned);
                    mTvStatus.setTextColor(this.getResources().getColor(com.ewei.sdk.R.color.white));
                    mTvStatus.setText("已分派");
                    break;
                default:
                    break;
            }
        }

        //处理人详情
        String deskName = "";
        if (mTicket.serviceDesk != null) {
            deskName = Utils.chgServiceDeskName(mTicket.serviceDesk.name);
        }

        if (mTicket.engineer != null && mTicket.engineer.user != null) {
            mTvHandler.setText(mTicket.engineer.user.name);
        } else {
            mTvHandler.setText(deskName);
        }
    }

    @Override
    protected void onClickAttachment(Attachment attachment, String localUrl, String fileType) {
        Intent intent;
        switch (fileType) {
            case TicketValue.TYPE_TICKET_REPLY_VIDEO:
                PlayVideoActivity.startActivity(this, attachment);
                break;
            case TicketValue.TYPE_TICKET_REPLY_IMG:
                SinglePreviewActivity.startActivity(this, attachment);
                break;
            default:
                intent = new Intent(this, AttachmentActivity.class);
                intent.putExtra(CommonValue.ATTACHMENT_INFO, attachment);
                startActivity(intent);
                break;
        }
    }

    @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 onClickChat(int chatId) {
        ChatUtils.startChatLogActivity(this, chatId, ChatLogActivity.class);
    }

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

    @Override
    protected Boolean isOpenLocation() {
        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;
            }
        }
    }

}
