package com.meeting.demo.view.scheme;

import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.meeting.demo.R;
import com.meeting.demo.utils.DemoUtil;

public class LinkLaunchFragment extends DialogFragment {
    public final static String TAG = LinkLaunchFragment.class.getSimpleName();
    View rootView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        rootView = inflater.inflate(R.layout.link_launch, container, false);

        Button launchBtn = rootView.findViewById(R.id.link_launch_btn);
        launchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launch();
            }
        });
        return rootView;
    }

    private void launch() {
        TextView schemeView = rootView.findViewById(R.id.link_launch_scheme);
        String scheme = schemeView.getText().toString();

        TextView authorityView = rootView.findViewById(R.id.link_launch_authority);
        String authority = authorityView.getText().toString();

        TextView pathView = rootView.findViewById(R.id.link_launch_path);
        String path = pathView.getText().toString();
        if (!TextUtils.isEmpty(scheme) && !TextUtils.isEmpty(authority) && !TextUtils.isEmpty(path)) {
            try {
                Uri.Builder builder = new Uri.Builder()
                        .scheme(scheme)
                        .authority(authority)
                        .path(path)
                        .appendQueryParameter("page","launch");
                DemoUtil.openAppWithURL(getContext(), builder.toString());
            } catch (UnsupportedOperationException e) {
                Log.e(TAG,e.getMessage());
            }
        }
    }
}
