package com.sobot.demo;

import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.sobot.chat.SobotApi;
import com.sobot.chat.SobotUIConfig;
import com.sobot.chat.api.apiUtils.SobotBaseUrl;
import com.sobot.chat.api.model.SobotTransferOperatorParam;
import com.sobot.chat.listener.SobotPlusMenuListener;
import com.sobot.chat.listener.SobotTransferOperatorInterceptor;
import com.sobot.chat.utils.LogUtils;
import com.sobot.chat.utils.ResourceUtils;
import com.sobot.chat.utils.SobotOption;
import com.sobot.chat.widget.kpswitch.view.ChattingPanelUploadView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/29.
 */

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        switchHost();
        LogUtils.isDebug = true;
        String appkey = SobotSPUtil.getStringData(this, "sobot_appkey", "");
        if (TextUtils.isEmpty(appkey)) {
            appkey = "07a5ff1050c047c4b9d3c57eeb7ced29";
        }
        SobotApi.initSobotSDK(this, appkey, SobotSPUtil.getStringData(this, "sobot_partnerId", ""));
        initUi();
        SobotApi.initPlatformUnion(this,"10001","");
//        customMenu();
//        initLocationModule();
//        initSendMsgModule();
        //initTransferInterceptor();
    }

    /**
     * 设置域名 如有更改域名需求才使用如下方法
     */
    private void switchHost() {
        String api_host = SobotSPUtil.getStringData(this, "sobot_api_host", "");
        if (!TextUtils.isEmpty(api_host)) {
            SobotBaseUrl.setHost(api_host);
        }
    }

    //自定义UI
    private void initUi() {
//        SobotUIConfig.sobot_serviceImgId = R.drawable.sobot_failed_normal;
//        SobotUIConfig.sobot_titleTextColor = R.color.sobot_color_evaluate_text_btn;
//        SobotUIConfig.sobot_moreBtnImgId  = R.drawable.sobot_btn_back_selector;
      //    SobotUIConfig.sobot_titleBgColor = R.color.sobot_text_delete_hismsg_color;
        //  SobotUIConfig.sobot_statusbar_BgColor = R.color.sobot_text_delete_hismsg_color;
//        SobotUIConfig.sobot_chat_bottom_bgColor = R.color.sobot_text_delete_hismsg_color;
//
//        SobotUIConfig.sobot_chat_left_textColor = R.color.sobot_text_delete_hismsg_color;
//        SobotUIConfig.sobot_chat_left_link_textColor = R.color.sobot_title_category_unselect_color;
//        SobotUIConfig.sobot_chat_left_bgColor = R.color.sobot_color_suggestion_history;
//
//        SobotUIConfig.sobot_chat_right_bgColor = R.color.sobot_title_category_unselect_color;
//        SobotUIConfig.sobot_chat_right_link_textColor = R.color.sobot_viewpagerbackground;
//        SobotUIConfig.sobot_chat_right_textColor = R.color.sobot_text_delete_hismsg_color;
//        SobotUIConfig.sobot_title_right_menu2_display = true;
//        SobotUIConfig.sobot_title_right_menu2_bg = R.drawable.sobot_failed_normal;

//        SobotUIConfig.sobot_title_right_menu2_display = true;
//        SobotUIConfig.sobot_title_right_menu2_bg = R.drawable.sobot_icon_call;
//        SobotUIConfig.sobot_title_right_menu2_call_num = "1111111";
    }

    private void customMenu() {
        /*ArrayList<ChattingPanelUploadView.SobotPlusEntity> objects = new ArrayList<>();
        objects.add(new ChattingPanelUploadView.SobotPlusEntity(R.drawable.sobot_camera_picture_button_selector, "位置", "action_location"));
        objects.add(new ChattingPanelUploadView.SobotPlusEntity(R.drawable.sobot_camera_picture_button_selector, "签到", "action_sing_in"));
        objects.add(new ChattingPanelUploadView.SobotPlusEntity(R.drawable.sobot_camera_picture_button_selector, "收藏", "action_ollection"));

        SobotUIConfig.pulsMenu.menus = objects;
        SobotUIConfig.pulsMenu.sSobotPlusMenuListener = new SobotPlusMenuListener() {
            @Override
            public void onClick(View view, String action) {
                ToastUtil.showToast(getApplicationContext(), "action:" + action);
            }
        };*/
    }

    //设置位置功能
    private void initLocationModule() {
        final String ACTION_LOCATION = "sobot_action_location";
        //拷贝assets中地图图片 到sdcard  demo的临时操作，实际使用时  需动态传入真实的图片地址
        IOUtils.copyAssetAndWrite(getApplicationContext(), "tmp_pic.jpg");
        //位置
        /*ChattingPanelUploadView.SobotPlusEntity locationEntity = new ChattingPanelUploadView.SobotPlusEntity(ResourceUtils.getDrawableId(getApplicationContext(), "sobot_location_btn_selector"), ResourceUtils.getResString(getApplicationContext(), "sobot_location"), ACTION_LOCATION);
        List<ChattingPanelUploadView.SobotPlusEntity> tmpList = new ArrayList<>();
        tmpList.add(locationEntity);
        SobotUIConfig.pulsMenu.operatorMenus = tmpList;
        SobotUIConfig.pulsMenu.sSobotPlusMenuListener = new SobotPlusMenuListener() {
            @Override
            public void onClick(View view, String action) {
                if (ACTION_LOCATION.equals(action)) {
                    Context context = view.getContext();
                    SobotLocationModel locationData = new SobotLocationModel();
                    //必须传入本地图片地址
                    locationData.setSnapshot(context.getCacheDir() + File.separator + "tmp_pic.jpg");
                    locationData.setLat("40.001630");
                    locationData.setLng("116.353313");
                    locationData.setLocalName("云景四季餐厅");
                    locationData.setLocalLabel("学清路38号金码大厦A座23层");

                    SobotApi.sendLocation(context, locationData);

//					view.getContext().startActivity(new Intent(view.getContext(),));
                }
            }
        };*/
    }

    //发送文字消息功能
    private void initSendMsgModule() {
        final String ACTION_SEND_MSG = "sobot_action_msg";
        ChattingPanelUploadView.SobotPlusEntity locationEntity = new ChattingPanelUploadView.SobotPlusEntity(ResourceUtils.getDrawableId(getApplicationContext(), "sobot_location_btn_selector"), "文字", ACTION_SEND_MSG);
        List<ChattingPanelUploadView.SobotPlusEntity> tmpList = new ArrayList<>();
        tmpList.add(locationEntity);
        SobotUIConfig.pulsMenu.operatorMenus = tmpList;
        SobotUIConfig.pulsMenu.sSobotPlusMenuListener = new SobotPlusMenuListener() {
            @Override
            public void onClick(View view, String action) {
                if (ACTION_SEND_MSG.equals(action)) {
                    Context context = view.getContext();
                    SobotApi.sendTextMsg(context, "订单号：123456789012\n" +
                            "\n" +
                            "商品1：恰恰瓜子200g*1\n" +
                            "\n" +
                            "商品链接：www.baidu.com\n" +
                            "\n" +
                            "商品描述：恰恰瓜子恰恰瓜子恰恰瓜子\n" +
                            "\n" +
                            "恰恰瓜子恰恰瓜子恰恰瓜子恰恰瓜子...\n" +
                            "\n" +
                            "商品2：农夫山泉500ml*12\n" +
                            "\n" +
                            "商品链接：www.sina.com\n" +
                            "\n" +
                            "商品描述:农夫山泉农夫山泉农夫山泉\n" +
                            "\n" +
                            "农夫山泉农夫山泉农夫山泉农夫山泉...");
                }
            }
        };
    }

    //配置转人工拦截器
    private void initTransferInterceptor() {
        SobotOption.transferOperatorInterceptor = new SobotTransferOperatorInterceptor() {
            @Override
            public void onTransferStart(final Context context, final SobotTransferOperatorParam param) {
                View rootView = View.inflate(context, R.layout.sobot_dialog_set_group_id, null);
                final EditText editText = rootView.findViewById(R.id.et_groupId);

                AlertDialog alertDialog = new AlertDialog.Builder(context)
                        .setView(rootView)
                        .setPositiveButton("转人工", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                                ConsultingContent consultingContent = new ConsultingContent(); //咨询内容
//                                consultingContent.setSobotGoodsTitle("乐视超级电视 S50 Air 全配版50英寸2D智能LED黑色（Letv S50 Air）"); //咨询内容标题
//                                consultingContent.setSobotGoodsImgUrl("http://www.lishui.com/uploads/140301/1-14030111052Y17.jpg"); //咨询内容图片 选填
//                                consultingContent.setSobotGoodsFromUrl("https://www.baidu.com/admin/order/detail/302"); //发送内容
//                                consultingContent.setSobotGoodsDescribe("乐视超级电视 S50 Air 全配版50英寸2D智能LED黑色（Letv S50 Air）乐视超级电视 S50 Air 全配版50英寸2D智能LED黑色（Letv S50 Air）");
//                                consultingContent.setSobotGoodsLable("￥2150");
//                                consultingContent.setAutoSend(true);
//                                param.setConsultingContent(consultingContent);
                                String groupId = editText.getText().toString();
                                if (!TextUtils.isEmpty(groupId)) {
                                    param.setGroupId(groupId);
                                }
                                SobotApi.transfer2Operator(context, param);

                            }
                        })
                        .setNegativeButton(ResourceUtils.getResStrId(context, "sobot_btn_cancle"), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create();
                alertDialog.show();
            }

        };
    }


}