package com.wd.workoffice.ui.activity.login;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.ui.activity.login.LoginActivity;
import com.wd.workoffice.ui.activity.mes.MesLoginActivity;
import com.wd.workoffice.ui.activity.user.UserPrivacyActivity;
import com.wd.workoffice.ui.activity.user.UserServerActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.UserKeeper;

import androidx.annotation.NonNull;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择角色
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MainLoginActivity extends WorkBaseActivity {


    @BindView(R.id.tv_bat)
    TextView tvBat;
    @BindView(R.id.tv_mes)
    TextView tvMes;
    @BindView(R.id.tv_server)
    TextView tvServer;
    @BindView(R.id.tv_privacy)
    TextView tvPrivacy;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        privacyDialog();
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_main_login;
    }


    @OnClick({R.id.tv_bat, R.id.tv_mes, R.id.tv_server, R.id.tv_privacy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bat:
//                startActivity(BatTypeActivity.class);
                //TODO 多账套需要修改
                startActivity(LoginActivity.class);
                break;
            case R.id.tv_mes:
                startActivity(MesLoginActivity.class);
                break;
            case R.id.tv_server:
                startActivity(UserServerActivity.class);
                break;
            case R.id.tv_privacy:
                startActivity(UserPrivacyActivity.class);
                break;
        }
    }
    private void privacyDialog() {
        QMUIDialog.CustomDialogBuilder msg = DialogUtils.customDialog(this);
        msg.setLayout(R.layout.view_privacy_dialog);
        QMUIDialog qmuiDialog = msg.create();
        qmuiDialog.setCancelable(false);
//        View viewDialog = LayoutInflater.from(this).inflate(R.layout.view_privacy_dialog, null, false);
//        qmuiDialog.setContentView(viewDialog);


        TextView tvGo = qmuiDialog.findViewById(R.id.tv_go);


        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append("你可阅读");

        String userAgreement = "《软件服务协议》";
        SpannableString userAgreementSpannable = new SpannableString(userAgreement);
        userAgreementSpannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //用户协议点击
                startActivity(UserServerActivity.class);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#1766DF"));
                ds.setUnderlineText(false);
            }
        }, 0, userAgreement.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        builder.append(userAgreementSpannable);
        builder.append("和");
        String privacyPolicy = "《隐私协议》";
        SpannableString privacyPolicySpannable = new SpannableString(privacyPolicy);
        privacyPolicySpannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                //隐私政策点击
                startActivity(UserPrivacyActivity.class);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#1766DF"));
                ds.setUnderlineText(false);//取消下划线
            }
        }, 0, privacyPolicySpannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        builder.append(privacyPolicySpannable);
        builder.append("了解详细信息。如你同意，请点击“同意”开始接受我们的服务。");
        tvGo.setMovementMethod(LinkMovementMethod.getInstance());
        tvGo.setHighlightColor(Color.parseColor("#1766DF"));//修改点击后的背景色
        tvGo.setText(builder);

        Button btnOk = qmuiDialog.findViewById(R.id.btn_ok);
        TextView tvCancel = qmuiDialog.findViewById(R.id.tv_cancel);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qmuiDialog.dismiss();
                UserKeeper.getInstance().keepFirstLogin(false);
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qmuiDialog.dismiss();
                finish();
            }
        });
        qmuiDialog.show();
    }

}
