package com.alipay.face.verify.fintechfacedemo;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VerifyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VerifyFragment extends Fragment {


    public boolean isDebug;
    private MainActivity mainActivity;
    private View rootView;

    public static VerifyFragment newInstance() {
        VerifyFragment fragment = new VerifyFragment();
        return fragment;
    }

    public VerifyFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        if (null != activity && activity instanceof MainActivity) {
            mainActivity = ((MainActivity) activity);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_verify, container, false);
        LinearLayout llCardType = rootView.findViewById(R.id.ll_card_type);
        LinearLayout llModeType = rootView.findViewById(R.id.ll_mode_type);
        LinearLayout llName = rootView.findViewById(R.id.ll_name);
        LinearLayout llBirthday = rootView.findViewById(R.id.ll_birthday);
        LinearLayout llValidate = rootView.findViewById(R.id.ll_validate);
        RadioButton rbWink = rootView.findViewById(R.id.rb_only_wink);

        mainActivity.etCertNo = ((EditText) rootView.findViewById(R.id.certNo));
        mainActivity.txtCertNo = rootView.findViewById(R.id.txt_cert_no);
        mainActivity.rbPassport = rootView.findViewById(R.id.rb_travel_passport);
        mainActivity.etBirthday = rootView.findViewById(R.id.birthday);
        mainActivity.etValidate = rootView.findViewById(R.id.validate);
        mainActivity.iOSLoading = rootView.findViewById(R.id.loading);
        mainActivity.rbIDCard = rootView.findViewById(R.id.rb_id_card);

        if (null != rbWink) {
            rbWink.setOnCheckedChangeListener((compoundButton, isChecked) -> {
                if (isChecked) {
                    mainActivity.setCurrentModel("LIVENESS");
                }
            });
        }
        RadioButton rbMultiAction = rootView.findViewById(R.id.rb_wink_multi_action);
        if (null != rbMultiAction) {
            rbMultiAction.setOnCheckedChangeListener((compoundButton, isChecked) -> {
                if (isChecked) {
                    mainActivity.setCurrentModel("MULTI_ACTION");
                }
            });
        }

        RadioButton rbPhotinusLiveness = rootView.findViewById(R.id.rb_wink_photinus_liveness);
        if (null != rbPhotinusLiveness) {
            rbPhotinusLiveness.setOnCheckedChangeListener((compoundButton, isChecked) -> {
                if (isChecked) {
                    mainActivity.setCurrentModel("PHOTINUS_LIVENESS");
                }
            });
        }

        RadioButton rbNearFar = rootView.findViewById(R.id.rb_near_far);
        if (null != rbNearFar) {
            rbNearFar.setOnCheckedChangeListener((compoundButton, isChecked) -> {
                if (isChecked) {
                    mainActivity.setCurrentModel("MOVE_ACTION");
                }
            });
        }

        Spinner spinner = rootView.findViewById(R.id.spinner_mode_name);
        if (null != spinner) {
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                    isDebug = position == 1;

                    rootView.findViewById(R.id.ll_exp).setVisibility(isDebug ? View.GONE : View.VISIBLE);
                    rootView.findViewById(R.id.btn_copy_certifyID).setVisibility(isDebug ? View.VISIBLE : View.GONE);
                    rootView.findViewById(R.id.ll_certifyID).setVisibility(isDebug ? View.VISIBLE : View.GONE);
                    rootView.findViewById(R.id.tv_showResult).setVisibility(isDebug ? View.GONE : View.VISIBLE);

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }

            });
        }

        Spinner spinnerProductType = rootView.findViewById(R.id.spinner_product_name);
        if (null != spinnerProductType) {
            spinnerProductType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position == 0) {
                        //活体
                        llCardType.setVisibility(View.GONE);
                        llBirthday.setVisibility(View.GONE);
                        llValidate.setVisibility(View.GONE);
                        llModeType.setVisibility(View.VISIBLE);
                        llName.setVisibility(View.VISIBLE);
                        mainActivity.setProductType(MainActivity.APP);
                    }
//                    else if (position == 1) {
//                        //OCR
//                        llCardType.setVisibility(View.GONE);
//                        llBirthday.setVisibility(View.GONE);
//                        llValidate.setVisibility(View.GONE);
//                        llModeType.setVisibility(View.VISIBLE);
//                        llName.setVisibility(View.VISIBLE);
//                        mainActivity.setProductType(MainActivity.OCR);
//                    }
                    else {
                        //NFC
                        llCardType.setVisibility(View.VISIBLE);
                        llBirthday.setVisibility(View.VISIBLE);
                        llValidate.setVisibility(View.VISIBLE);
                        llModeType.setVisibility(View.GONE);
                        llName.setVisibility(View.GONE);
                        mainActivity.setProductType(MainActivity.NFC);
                    }

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
        mainActivity.rbPassport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mainActivity.rbPassport.isChecked()) {
                    mainActivity.etValidate.setHint("必填");
                    mainActivity.etBirthday.setHint("必填");
                } else {
                    mainActivity.etValidate.setHint("");
                    mainActivity.etBirthday.setHint("");
                }
            }
        });

        return rootView;
    }


}