package com.bokecc.dwlivedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bokecc.dwlivedemo.DWApplication;
import com.bokecc.dwlivedemo.R;
import com.bokecc.dwlivedemo.base.BaseActivity;
import com.bokecc.dwlivedemo.scan.qr_codescan.MipcaActivityCapture;
import com.bokecc.dwlivedemo.view.LoginLineLayout;
import com.bokecc.livemodule.view.CustomToast;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author: cc
 * Date: 2024/1/8
 * Description:
 */
public class LocalReplayVerifyActivity extends BaseActivity {

    private static final String TAG = "LocalReplayVerifyActivity";

    private static final int QR_REQUEST_CODE = 222;


    private ImageView ivBack;
    private ImageView ivScan;
    private LoginLineLayout lllEnterLocalReplayKey;
    private Button btnEnterLocalReplay;
    private boolean isVideoMain;
    private String fileName;
    private LoginLineLayout lllEnterLocalReplayVideoId;

    public static void openActivity(DownloadListActivity activity, boolean isVideoMain, String fileName) {
        Intent intent = new Intent(activity, LocalReplayVerifyActivity.class);
        intent.putExtra("isVideoMain", isVideoMain);
        intent.putExtra("fileName", fileName);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_local_replay_verify);

        Intent intent = getIntent();
        if (intent!=null){
            isVideoMain = intent.getBooleanExtra("isVideoMain",false);
            fileName = intent.getStringExtra("fileName");
        }
        if (TextUtils.isEmpty(fileName)){
            toastOnUiThread("fileName is null");
            finish();
            return;
        }

        initView();
        initListener();
    }
    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivScan = findViewById(R.id.iv_scan);
        lllEnterLocalReplayKey = findViewById(R.id.lll_enter_local_replay_key);
        lllEnterLocalReplayVideoId = findViewById(R.id.lll_enter_local_replay_video_id);
        lllEnterLocalReplayVideoId.setText("");
        lllEnterLocalReplayKey.setText("");
        btnEnterLocalReplay = findViewById(R.id.btn_enter_local_replay);
    }
    private void initListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showScan();
            }
        });
        btnEnterLocalReplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DWApplication.getInstance().isInPictureInPictureMode()) {
                    toastOnUiThread("请先退出画中画模式！");
                    return;
                }
                String key = lllEnterLocalReplayKey.getText();
                String videoId = lllEnterLocalReplayVideoId.getText();
                if (TextUtils.isEmpty(key)) {
                    CustomToast.showToast(getApplicationContext(), "请输入播放密钥！", Toast.LENGTH_SHORT);
                    return;
                }
                // 启动离线回放页面
                LocalReplayPlayActivity.openActivity(LocalReplayVerifyActivity.this,isVideoMain, fileName,key,videoId);
            }
        });
    }



    private void showScan() {
        Intent intent = new Intent(this, MipcaActivityCapture.class);
        startActivityForResult(intent, QR_REQUEST_CODE);
    }

    private Map<String,String> map;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        //http://ccr.csslcloud.net/6D41E681B274AB66/5BEE1BB7231D25D49C33DC5901307461/8B770156D3F326899C33DC5901307461/v2.ccr?userId=6D41E681B274AB66&videoId=96B71498AFDAF3BA9C33DC5901307461&pcmToken=125,125,120,9,125,14,14,120,127,15,9,9,0,10,14,124,0,123,1,14,0,122,11,120,13,11,12,15,12,14,0,8

        if (requestCode == QR_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Bundle bundle = data.getExtras();
                String result = bundle.getString("result");

                if (TextUtils.isEmpty(result)) {
                    CustomToast.showToast(getApplicationContext(), "扫描失败，请扫描正确的二维码", Toast.LENGTH_SHORT);
                    return;
                }
                map = parseUrl(result);
                if (map != null) {
                    String videoId = map.get("videoId");
                    String pcmToken = map.get("pcmToken");
                    if (TextUtils.isEmpty(videoId) || TextUtils.isEmpty(pcmToken) ) {
                        CustomToast.showToast(getApplicationContext(), "扫描失败，请扫描正确的二维码", Toast.LENGTH_SHORT);
                        return;
                    }
                    CustomToast.showToast(getApplicationContext(), "扫码成功！", Toast.LENGTH_SHORT);
                    lllEnterLocalReplayVideoId.setText(videoId);
                    lllEnterLocalReplayKey.setText(pcmToken);
                }
            }
        }
    }


    private Map<String, String> parseUrl(String url) {
        Map<String, String> map = new HashMap<String, String>();
        String param = url.substring(url.indexOf("?") + 1, url.length());
        String[] params = param.split("&");

        if (params.length < 2) {
            return null;
        }
        for (String p : params) {
            String[] en = p.split("=");
            if (en.length < 2) {
                map.put(en[0], "");
            } else {
                map.put(en[0], en[1]);
            }

        }
        return map;
    }

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

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


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

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

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