package com.alipay.face.verify.fintechfacedemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;


public class ResultFragment extends Fragment {

    private MainActivity mainActivity;
    private JSONObject jsonNfcInfo = null;
    private String result = "";

    public ResultFragment() {
    }

    public static ResultFragment newInstance(String strResult) {
        ResultFragment fragment = new ResultFragment();
        Bundle bundle = new Bundle();
        bundle.putString("result", strResult);
        fragment.setArguments(bundle);
        return fragment;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_result, container, false);
        if (null != rootView) {
            rootView.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mainActivity.showVerifyFragment();
                }
            });
            Bundle bundle = getArguments();
            result = bundle.getString("result");
            JSONObject jsonResult = JSONObject.parseObject(result);
            JSONObject jsonData = jsonResult.getJSONObject("data");
            JSONObject jsonMaterialInfo = jsonData.getJSONObject("materialInfo");
            TextView txtVerifyResult = rootView.findViewById(R.id.txt_result);
            if (null != txtVerifyResult) {
                String passed = jsonData.getString("passed");
                txtVerifyResult.setText("T".equalsIgnoreCase(passed) ? "通过" : "不通过");
            }
            if (mainActivity.getProductType() == MainActivity.NFC) {
                LinearLayout llAPP = rootView.findViewById(R.id.ll_app);
                llAPP.setVisibility(View.GONE);
                RecyclerView rvNFCResult = rootView.findViewById(R.id.rv);
                jsonNfcInfo = jsonMaterialInfo.getJSONObject("nfcInfo");
                ResultAdapter adapter = new ResultAdapter(mainActivity, jsonNfcInfo);
                LinearLayoutManager manager = new LinearLayoutManager(mainActivity);
                rvNFCResult.setLayoutManager(manager);
                DividerItemDecoration divider = new DividerItemDecoration(mainActivity, manager.getOrientation());
                rvNFCResult.addItemDecoration(divider);
                rvNFCResult.setAdapter(adapter);

                dealWithNfcBase64Image("picture", "docImage", "docImage2");
                jsonMaterialInfo.put("nfcInfo", jsonNfcInfo);
                jsonData.put("materialInfo", jsonMaterialInfo);
                jsonResult.put("data", jsonData);
                result = jsonResult.toJSONString();
            } else {
                TextView txtCompareResault = rootView.findViewById(R.id.txt_compare_result);
                if (null != txtCompareResault) {
                    String samePerson = jsonMaterialInfo.getString("samePerson");
                    txtCompareResault.setText("T".equalsIgnoreCase(samePerson) ? "通过" : "不通过");
                }
                TextView txtLiveness = rootView.findViewById(R.id.txt_liveness_result);
                if (null != txtLiveness) {
                    String livenessPassed = jsonMaterialInfo.getString("livenessPassed");
                    txtLiveness.setText("true".equalsIgnoreCase(livenessPassed) ? "活体通过" : "活体不通过");
                }
                TextView txtRiskInterceptionType = rootView.findViewById(R.id.txt_risk_type);
                if (null != txtRiskInterceptionType) {
                    String riskInterceptionType = jsonMaterialInfo.getString("riskInterceptionType");
                    switch (riskInterceptionType) {
                        case "LIVENESS_NOT_PASS":
                            txtRiskInterceptionType.setText("NA");
                            break;
                        case "STRATEGY_NOT_PASS":
                            txtRiskInterceptionType.setText("策略不通过");
                            break;
                        default:
                            txtRiskInterceptionType.setText("策略通过");
                            break;
                    }
                }
            }
            Button btnDetail = rootView.findViewById(R.id.btn_detail);
            if (null != btnDetail) {
                btnDetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mainActivity.showAlert("认证提示", result);
                    }
                });
            }
        }
        return rootView;
    }

    public void dealWithNfcBase64Image(String... keys) {
        for (String key : keys) {
            if (jsonNfcInfo.containsKey(key)) {
                jsonNfcInfo.put(key, jsonNfcInfo.getString(key).substring(0, 10) + "...");
            }
        }
    }
}