package com.sobot.moduletest;


import static com.sobot.widget.ui.SobotBaseConstant.REQUEST_CODE_PICTURE;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.TextView;

import com.sobot.common.utils.SobotImageUtils;
import com.sobot.pictureframe.SobotBitmapUtil;
import com.sobot.utils.SobotLogUtils;
import com.sobot.utils.SobotSDCardUtils;
import com.sobot.widget.SobotWidgetApi;
import com.sobot.widget.image.photoview.SobotPhotoView;
import com.sobot.widget.loading.SobotLoadingLayout;
import com.sobot.widget.refresh.layout.api.RefreshLayout;
import com.sobot.widget.refresh.layout.footer.ClassicsFooter;
import com.sobot.widget.refresh.layout.header.ClassicsHeader;
import com.sobot.widget.ui.SobotMarkConfig;
import com.sobot.widget.ui.base.SobotBaseActivity;
import com.sobot.widget.ui.rich.HtmlToolUtils;
import com.sobot.widget.ui.toast.SobotToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends SobotBaseActivity {

    private ImageView img;
    private SobotPhotoView img2;
    private SobotLoadingLayout loadinglayout;
    private TextView tv;


    @Override
    protected int getContentViewResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tv = findViewById(R.id.tv);
        HtmlToolUtils.getInstance(MainActivity.this).setRichText(tv, "<h3><span style=\"color: rgb(255, 77, 79);\"><strong>【若摄像机因出现自动呼叫导致设备离线，建议您</strong></span><span style=\"color: rgb(0, 0, 0);\"><strong>优先更换电</strong></span><span style=\"color: rgb(255, 77, 79);\"><strong>源适配器尝试。若</strong></span><span style=\"color: rgb(235, 144, 58);\"><strong>设备恢复正常建</strong></span><span style=\"color: rgb(255, 77, 79);\"><strong>议联系在</strong></span><span style=\"color: rgb(115, 209, 61);\"><strong>线人工客服</strong></span><span style=\"color: rgb(255, 77, 79);\"><strong>咨询（早9-晚21），</strong></span><span style=\"color: rgb(38, 38, 38);\"><strong>若仍无法解决</strong></span><span style=\"color: rgb(255, 77, 79);\"><strong>建议您按以下方式排查】</strong></span></h3><h2>您好，若是摄像机联网成功后，突然离线，建议您按以下方式排查：一、离线后，请确认最近摄像机环境是否有变化，是否更换过路由器，目前仅<span style=\"color: rgb(225, 60, 57);\">AB2L、P8MAX、K6pro、P8 pro</span>型号摄像机支持5GWiFi，其余型号摄像机需连接2.4G网络。请查看摄像机信息里面的WIFI信号强度（尽可能大于80%），检查摄像机所处环境中电源、网络是否正常。</h2><h2>二、重新插拔电源线，如果设备没有问题，18510555567每次通电后，都是先绿灯常亮然后是绿灯闪烁。如果不亮，可能是适配器电源线或者摄像机有问题，这种情况建议您更换适配器或电源线，还不行则需要寄回售后检测维修。</h2><p>三、建议您重新连接（如果有安装内存卡，建议取出内存卡，不排除内存卡损坏导致设备离线的可能），重新连接方式如下：</p><p>1）直接点击右上角加号，选择“连接我的摄像机”2）摄像机设置中，选择“让摄像机连接到其他WIFI”</p><p>四、建议更换网络WiFi或连接手机热点尝试。五、查看固件 <a href=\"https://www.baidu.com\" target=\"_blank\">百度</a> 版本是否最新，若不是建议升级固件，并重新联网尝试。</p><p>若仍无法解决，可点击【自助报修】或通过“360智慧生活服务”公众号报修。&lt;/p&gt;</p>", R.color.sobot_common_green);
        SobotToastUtil.showCustomToast(getSobotBaseActivity(), "sdafdsafsadfasdfsdaf");
        SobotWidgetApi.setSwitchMarkStatus(SobotMarkConfig.SHOW_PERMISSION_TIPS_POP, true);
        setTitle("ddddd");
        SobotBitmapUtil.display(getSobotBaseActivity(), "https://img.sobot.com/console/common/face/admin.png", getAvatarImageView(true));
        img = findViewById(R.id.img);
        SobotBitmapUtil.display(this, "https://img.sobot.com/console/common/face/admin.png", img, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        img2 = findViewById(R.id.img2);

//        img2.setScaleType(ImageView.ScaleType.FIT_XY);
        SobotBitmapUtil.display(this, "https://img.sobot.com/chatres/75574e5fa29a48458d1f57ab5489a4c5/msg/20220421/34a466420d78fa1172bc0acd85166745/9410b53be86b4d589c61c60f6aee2461.jpeg", img2);
//        File save=new File(SobotSDCardUtils.getSDCardRootPath(MainActivity.this)+"/9410b53be86b4d589c61c60f6aee2461.jpeg");
//        HttpBaseUtils.getInstance().download("https://img.sobot.com/chatres/75574e5fa29a48458d1f57ab5489a4c5/msg/20220421/34a466420d78fa1172bc0acd85166745/9410b53be86b4d589c61c60f6aee2461.jpeg", save, new HttpBaseUtils.FileCallBack() {
//            @Override
//            public void onResponse(File result) {
//                Bitmap bitmap= SobotBitmapUtil.compress(result.getPath(),MainActivity.this,true);
//                img2.setImageBitmap(bitmap);
//            }
//
//            @Override
//            public void onError(Exception e, String msg, int responseCode) {
//                SobotLogUtils.i("下载失败："+msg);
//            }
//
//            @Override
//            public void inProgress(int progress) {
//SobotLogUtils.i("下载进度："+progress);
//            }
//        });
//
//        SobotDownloadManager.getInstance().get("sdfsdfds");
//
//        Map<String, String> map = new HashMap<>();
//        map.put("number", "142160000");
//        map.put("companyId", "1eae128b7e394aedafd28828bfa84767");
//        map.put("agentId", "969a7ee9fd4c42f48171b0128df2f16c");
//        map.put("displayNumber", "01051393569");
//        map.put("callWay", "2");
//        map.put("isRecordStereo", "false");
//
//        map.put("agentState", "1");
//        map.put("appId", "a4b246c8b67e4543b3b870ce62f75546");
//        map.put("groupId", "36b0f8eb2de948d99e89c8f0c3d92e68_5");
//        map.put("agentPhoneNum", "18600978969");
//
//        SobotSharedPreferencesUtil.getInstance(this).put("aa",map);
//        Map<String, String> map1=SobotSharedPreferencesUtil.getInstance(this).get("aa",Map.class);
//        SobotSharedPreferencesUtil.getInstance(this).clearAll();
//        Map<String, String> map2=SobotSharedPreferencesUtil.getInstance(this).get("aa",Map.class);
//
//        SobotSharedPreferencesUtil.getInstance(this).put("aa",map);
//        Map<String, String> map3=SobotSharedPreferencesUtil.getInstance(this).get("aa",Map.class);
//        SobotSharedPreferencesUtil.getInstance(this).remove("aa");
//        Map<String, String> map4=SobotSharedPreferencesUtil.getInstance(this).get("aa",Map.class);
//
//        SobotHttpUtils.doPostByJsonString(this, "https://sobot.com/comm/api/4/call/agentOnWork?access_token=c273b74c-3b37-48cf-937f-9c46940cc19d", map, null, new HttpBaseUtils.StringCallBack() {
//            @Override
//            public void onResponse(String result) {
//                SobotLogUtils.i(result);
//            }
//
//            @Override
//            public void onError(Exception e, String msg, int responseCode) {
//
//            }
//
//            @Override
//            public void inProgress(int progress) {
//
//            }
//        });
//        selectPicFromLocal();
//        selectVedioFromLocal();
//        openCamera();

        RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setEnableLoadMore(false);

        loadinglayout = findViewById(R.id.loadinglayout);
        loadinglayout.showContent();
        Map<String, Object> map = new HashMap<>();
        map.put("uid", "34a466420d78fa1172bc0acd85166745");
        map.put("robotFlag", "1");
        map.put("faqId", "0");
        map.put("version", "2.9.7");
        map.put("from", "2");
        Map<String, String> headers = new HashMap<>();
        headers.put("version", "2.9.7");
        headers.put("from", "2");
//        SobotHttpUtils.doPost(MainActivity.this, "https://api.sobot.com/chat-sdk/sdk/user/v2/robotGuide.action", 5000,map, headers, new HttpBaseUtils.StringCallBack() {
//            @Override
//            public void onResponse(String result) {
//            }
//
//            @Override
//            public void onError(Exception e, String msg, int responseCode) {
//
//            }
//
//            @Override
//            public void inProgress(int progress) {
//
//            }
//        });
        String languageFileName = "sobot_android_strings_en.json";
        String languagePath = SobotSDCardUtils.getPrivatePath(MainActivity.this) + File.separator + "sobot_language" + File.separator + languageFileName;
        File file = new File(languagePath);
//        HttpBaseUtils.getInstance().download("https://img.sobot.com/mobile/multilingual/android/3_1_5/sobot_android_strings_en.json", file, new HttpBaseUtils.FileCallBack() {
//            @Override
//            public void onResponse(File result) {
//                if (result.exists()) {
//                    SobotLogUtils.i("下载文件成功");
//                }else{
//                    SobotLogUtils.i("下载的文件不存在");
//                }
//            }
//
//            @Override
//            public void onError(Exception e, String msg, int responseCode) {
//
//            }
//
//            @Override
//            public void inProgress(int progress) {
//
//            }
//        });

//        if (file.exists()) {
//            SobotLogUtils.i("文件存在 开始上传");
//            Map<String, Object> map1 = new HashMap<>();
//            map1.put("uid", "34a466420d78fa1172bc0acd85166745");
//            map1.put("cid", "2b3dd49e54654313a2985c1a160f1671");
//            map1.put("from", "2");
//            map1.put("version", "2.9.7");
//            Map<String, String> headers1 = new HashMap<>();
//            SobotHttpUtils.uploadFile(MainActivity.this, "https://api.sobot.com/chat-sdk/sdk/user/v2/uploadFileAndroid.action", map1, headers1, file.getAbsolutePath(), new HttpBaseUtils.StringCallBack() {
//                @Override
//                public void onResponse(String result) {
//
//                }
//
//                @Override
//                public void onError(Exception e, String msg, int responseCode) {
//
//                }
//
//                @Override
//                public void inProgress(int progress) {
//
//                }
//            });
//        }

        Map<String, Object> map1 = new HashMap<>();
        map1.put("email", "zhengnw@sobot.com");
        map1.put("passwd", "znw123456");
        map1.put("loginStatus", "1");
        map1.put("from", 2 + "");
        map1.put("way", "10");
        map1.put("ack", "1");
        Map<String, String> headers1 = new HashMap<>();
        headers1.put("language", "zh");

//        SobotLoginTools.getInstance().doAppLogin(MainActivity.this, MainActivity.this, "https://api-c.sobot.com/text","https://api-c.soboten.com/text", 8000, map1, headers1, new SobotResultBlock() {
//            @Override
//            public void resultBolok(SobotResultCode code, String msg, Object obj) {
//                SobotLoginTools.getInstance().checkExpiresToken();
//                SobotLogUtils.i(SobotLoginTools.getInstance().getAccessToken()+"");
//                SobotLogUtils.i(SobotLoginTools.getInstance().getTempId());
//                SobotLogUtils.i(SobotLoginTools.getInstance().getToken()+"");
//            }
//        });
//        LoginUserEntity sa=new LoginUserEntity();
//        sa.setLogin_time(System.currentTimeMillis());
//        SobotLoginTools.getInstance().setLoginUser(sa);
//        SobotServiceInfoModel ccc=new SobotServiceInfoModel();
//        SobotLoginTools.getInstance().setServiceInfo(ccc);

//        SobotLoginTools.getInstance().doLogin(MainActivity.this, MainActivity.this, "https://api-c.sobot.com/text","https://api-c.soboten.com", "zhengnw@sobot.com", "znw123456","", new SobotResultBlock() {
//            @Override
//            public void resultBolok(SobotResultCode code, String msg, Object obj) {
//                SobotLoginTools.getInstance().checkExpiresToken();
//                SobotLogUtils.i(SobotLoginTools.getInstance().getAccessToken()+"");
//                SobotLogUtils.i(SobotLoginTools.getInstance().getTempId());
//                SobotLogUtils.i(SobotLoginTools.getInstance().getToken()+"");
//                SobotLogUtils.i(SobotLoginTools.getInstance().getServiceInfo().getTimezone()+"----------------------------------"+SobotLoginTools.getInstance().getServiceInfo().getTimezoneId());
//            }
//        });
    }

    @Override
    protected void initData() {
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);
            SobotLogUtils.i("多媒体返回的结果：" + requestCode + "--" + resultCode + "--" + data);

            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == REQUEST_CODE_PICTURE) { // 发送本地图片
                    if (data != null && data.getData() != null) {
                        Uri selectedImage = data.getData();
                        if (selectedImage == null) {
                            selectedImage = SobotImageUtils.getUri(data, getBaseContext());
                        }
                        String path = SobotImageUtils.getPath(getSobotBaseActivity(), selectedImage);
                        SobotLogUtils.i("选取的图片地址：" + path);
                    }
                }
            }
            if (cameraFile != null && cameraFile.exists()) {
                SobotLogUtils.i("相机拍照：" + cameraFile.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}