package com.mogo.cloud;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.mogo.cloud.network.BaseData;
import com.mogo.cloud.tanlu.api.MogoRoadSearchManager;
import com.mogo.cloud.tanlu.api.MogoUploadManager;
import com.mogo.cloud.tanlu.api.IRoadInfoSearchCallback;
import com.mogo.cloud.tanlu.api.ITanluUploadCallback;
import com.mogo.cloud.tanlu.bean.RoadInfoRequest;
import com.mogo.cloud.tanlu.bean.RoadInfos;
import com.mogo.cloud.tanlu.bean.UploadInfo;
import com.mogo.cloud.tanlu.bean.UploadResult;
import com.mogo.cloud.tanlu.bean.location.Location;

import java.util.ArrayList;

/**
 * @author lixiaopeng
 * @description 路况服务
 * @since: 2021/1/21
 */
public class RoadConditionActivity extends AppCompatActivity {
    private static final String TAG = "RoadConditionActivity";
    private Button roadUploadButton;
    private Button roadQueryButton;
    private TextView roadUploadTv;
    private TextView roadQueryTv;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_roadcondition);

        roadUploadTv = findViewById(R.id.road_upload_tv);
        roadQueryTv = findViewById(R.id.road_query_tv);

        roadUploadButton = findViewById(R.id.road_upload_bt);
        roadUploadButton.setOnClickListener(view -> {
            uploadRoadInfo();
        });

        roadQueryButton = findViewById(R.id.road_query_bt);
        roadQueryButton.setOnClickListener(view -> {
            queryRoad();
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void queryRoad() {
        ArrayList list = new ArrayList();
        list.add("10007");

        //北京市朝阳区小黄庄北街2号， 经纬度和地理位置必须有一个
        Location location = new Location();
        location.setLatitude(39.968139);
        location.setLongitude(116.411468);
        RoadInfoRequest request = new RoadInfoRequest(location, "中关村", list, false, false);

        MogoRoadSearchManager.getInstance(RoadConditionActivity.this).init();
        MogoRoadSearchManager.getInstance(RoadConditionActivity.this).queryRoadInfo(request, new IRoadInfoSearchCallback() {
            @Override
            public void onSuccess(BaseData<RoadInfos> result) {
                if (result != null && result.getResult() != null) {
                    if (result != null && result.getResult() != null) {
                        roadQueryTv.setText(new Gson().toJson(result));
                    } else {
                        roadQueryTv.setText("onSuccess code = " + result.getCode() + " --message = " + result.getMsg());
                    }
                }
            }

            @Override
            public void onFailure(int code) {
                roadQueryTv.setText("onFailure code = " + code);
            }

            @Override
            public void onError(Throwable e) {
                roadQueryTv.setText("onError e = " + e);
            }
        });
    }

    public void uploadRoadInfo() {
        UploadInfo info = new UploadInfo();
//        info.setFilePath("/storage/emulated/0/Movies/compress_video_20210127112035.mp4");
        info.setFilePath("/storage/emulated/0/Movies/2222.png");

        info.setAddr("北京市东城区小黄庄北街2号靠近中国银行");
        info.setLongitude(116.410892);
        info.setLatitude(39.968317);
        info.setPoiType("10007");
        info.setDirection(0);
        info.setAreaCode("110101");
        info.setCityCode("010");
        info.setSn("F803EB2046PZD00228");
        info.setType(0); //0为图片， 1为视频

        MogoUploadManager.getInstance(RoadConditionActivity.this).uploadInfo(info, new ITanluUploadCallback() {
            @Override
            public void onSuccess(BaseData<UploadResult> result) {
                if (result != null && result.getResult() != null) {
                    roadUploadTv.setText("onSuccess id = "  + result.getResult().id + "");
                } else {
                    roadUploadTv.setText("onSuccess code = " + result.getCode() + " --message = " + result.getMsg());
                }
            }

            @Override
            public void onFailure(int code) {
                roadUploadTv.setText("onFailure code = " + code);
            }

            @Override
            public void onError(Throwable e) {
                roadUploadTv.setText("onError e = " + e);
            }
        });
    }


}
