package com.cooleshow.teacher.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.teacher.R;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;


/**
 * Author by pq, Date on 2022/8/24.
 */
public class HomeCertTipDialog extends Dialog {
    private ProgressBar mProgress;
    private ConstraintLayout mCsContent;
    private ImageView mIvEducationCertStatus;
    private TextView mTvEducationCertStatus;
    private ImageView mIvTeachersCertStatus;
    private TextView mTvTeachersCertStatus;
    private TextView mTvProgress;
    private TextView mTvGoCert;

    public HomeCertTipDialog(@NonNull Context context) {
        super(context, com.cooleshow.base.R.style.BaseDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_home_cert_tip_layout);
        mProgress = findViewById(R.id.progress);
        mCsContent = findViewById(R.id.cs_content);
        mIvEducationCertStatus = findViewById(R.id.iv_education_cert_status);
        mTvEducationCertStatus = findViewById(R.id.tv_education_cert_status);
        mIvTeachersCertStatus = findViewById(R.id.iv_teachers_cert_status);
        mTvTeachersCertStatus = findViewById(R.id.tv_teachers_cert_status);
        mTvProgress = findViewById(R.id.tv_progress);
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mTvGoCert = findViewById(R.id.tv_go_cert);
        mTvGoCert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_HTML)
                        .withString(WebConstants.WEB_URL, WebConstants.TEACHER_OTHER_CERT)
                        .navigation();
                dismiss();
            }
        });
    }

    public void setData(int degreeFlag, int teacherFlag) {
        int certNum = 0;
        if (degreeFlag == 1) {
            certNum++;
            mIvEducationCertStatus.setImageResource(R.drawable.icon_check_select);
            mTvEducationCertStatus.setText("已认证");
            mTvEducationCertStatus.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_999999));
        } else {
            mIvEducationCertStatus.setImageResource(com.cooleshow.base.R.drawable.shape_2dc7aa_ring_border_1dp);
            mTvEducationCertStatus.setText("未认证");
            mTvEducationCertStatus.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff0000));
        }
        if (teacherFlag == 1) {
            certNum++;
            mIvTeachersCertStatus.setImageResource(R.drawable.icon_check_select);
            mTvTeachersCertStatus.setText("已认证");
            mTvTeachersCertStatus.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_999999));
        } else {
            mIvTeachersCertStatus.setImageResource(com.cooleshow.base.R.drawable.shape_2dc7aa_ring_border_1dp);
            mTvTeachersCertStatus.setText("未认证");
            mTvTeachersCertStatus.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_ff0000));
        }
        setProgress(certNum == 0 ? 80 : certNum == 2 ? 100 : 90);
    }

    private void setProgress(int progress) {
        if (progress != 100) {
            mTvGoCert.setText("去认证");
        } else {
            mTvGoCert.setText("我知道了");
        }
        if (mProgress != null) {
            float value = progress / 100f;
            mProgress.setProgress(progress);
            mTvProgress.setText(progress + "%");
            ConstraintSet set = new ConstraintSet();
            set.clone(mCsContent);
            set.setHorizontalBias(R.id.view_help, value);
            set.applyTo(mCsContent);
        }
    }
}
