package com.tencent.trtc.videocall;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.trtc.debug.Constant;

/**
 * TRTC视频通话的入口页面（可以设置房间id和用户id）
 *
 * - 可跳转TRTC视频通话页面{@link VideoCallingActivity}
 */

/**
 * Video Call Entrance View (set room ID and user ID)
 *
 * - Direct to the video call view: {@link VideoCallingActivity}
 */
public class VideoCallingEnterActivity extends AppCompatActivity {

    private EditText mEditInputUserId;
    private EditText mEditInputRoomId;

    private int getId(String idName, String type) {
        return getResources().getIdentifier(idName, type, getPackageName());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.videocall_activit_enter);
        setContentView(getId("videocall_activit_enter", "layout"));
        getSupportActionBar().hide();
//        mEditInputUserId = findViewById(R.id.et_input_username);
//        mEditInputRoomId = findViewById(R.id.et_input_room_id);
        mEditInputUserId = findViewById(getId("et_input_username", "id"));
        mEditInputRoomId = findViewById(getId("et_input_room_id", "id"));
//      R.id.btn_enter_room
        findViewById(getId("btn_enter_room", "id")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startEnterRoom();
            }
        });
//      R.id.rl_entrance_main
        findViewById(getId("rl_entrance_main", "id")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideInput();
            }
        });
//      R.id.iv_back
        findViewById(getId("iv_back", "id")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mEditInputRoomId.setText("1256732");
        String time = String.valueOf(System.currentTimeMillis());
        String userId = time.substring(time.length() - 8);
        mEditInputUserId.setText(userId);
    }

    private void startEnterRoom() {
        if (TextUtils.isEmpty(mEditInputUserId.getText().toString().trim())
                || TextUtils.isEmpty(mEditInputRoomId.getText().toString().trim())) {
//          R.string.videocall_room_input_error_tip
            Toast.makeText(VideoCallingEnterActivity.this, getString(getId("videocall_room_input_error_tip", "string")), Toast.LENGTH_LONG).show();
            return;
        }
        Intent intent = new Intent(VideoCallingEnterActivity.this, VideoCallingActivity.class);
        intent.putExtra(Constant.ROOM_ID, mEditInputRoomId.getText().toString().trim());
        intent.putExtra(Constant.USER_ID, mEditInputUserId.getText().toString().trim());
        startActivity(intent);
    }

    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

}
