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 LinkLoginFragment extends DialogFragment {
    public final static String TAG = LinkLoginFragment.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_login, container, false);

        Button loginBtn = rootView.findViewById(R.id.link_login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        return rootView;
    }

    private void login() {
        TextView addressView = rootView.findViewById(R.id.linklogin_server_address);
        String serverAddress = addressView.getText().toString();
        TextView portView = rootView.findViewById(R.id.linklogin_server_port);
        String portString = portView.getText().toString();
        if (portString.equals("")) {
            portString = portView.getHint().toString();
        }
        TextView companyDomainView = rootView.findViewById(R.id.linklogin_company_domain);
        String domain = companyDomainView.getText().toString();
        TextView oauthCodeView = rootView.findViewById(R.id.linklogin_code);
        String code = oauthCodeView.getText().toString();

        TextView schemeView = rootView.findViewById(R.id.linklogin_scheme);
        String scheme = schemeView.getText().toString();

        TextView authorityView = rootView.findViewById(R.id.linklogin_authority);
        String authority = authorityView.getText().toString();
        if (!serverAddress.isEmpty() && !portString.isEmpty() && !domain.isEmpty() && !code.isEmpty()
                && !TextUtils.isEmpty(scheme) && !TextUtils.isEmpty(authority)) {
            try {
                Uri.Builder builder = new Uri.Builder()
                        .scheme(scheme)
                        .authority(authority)
                        .path("h5page")
                        .appendQueryParameter("page","ssoLogin")
                        .appendQueryParameter("server_url",serverAddress)
                        .appendQueryParameter("port",portString)
                        .appendQueryParameter("domain",domain)
                        .appendQueryParameter("code",code);
                DemoUtil.openAppWithURL(getContext(), builder.toString());
            } catch (UnsupportedOperationException e) {
                Log.e(TAG,e.getMessage());
            }
        }
    }
}
