package com.tjzhxx.union.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.tjzhxx.union.MainActivity;
import com.tjzhxx.union.R;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.SpUtils;
import com.tjzhxx.union.system.BaseActivity;
import com.tjzhxx.union.system.WebviewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PrivacyRemarkDialog extends Dialog {
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.remark)
    TextView remark;
    private Context context;

    OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public PrivacyRemarkDialog(Context context) {
        super(context, R.style.Dialog);
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_privacy_remark, null);
        setContentView(view);
        ButterKnife.bind(this);
        setCancelable(false);
        initString();
    }


    @OnClick({R.id.ll_agree, R.id.left, R.id.right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_agree:
                checkbox.setChecked(!checkbox.isChecked());
                break;
            case R.id.left:
                dismiss();
                ((MainActivity) context).finish();
                break;
            case R.id.right:
                if (checkbox.isChecked()) {
                    dismiss();
                    SpUtils.put(context, "PrivacyRemark", false);
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onRight();
                    }
                } else {
                    ((BaseActivity) context).showSnackBar("请先勾选同意事项");
                }
                break;
        }
    }

    private void initString() {
        String s01 = context.getResources().getString(R.string.privacy_ramark_02);
        String s02 = context.getResources().getString(R.string.privacy_ramark_03);
        String s03 = context.getResources().getString(R.string.privacy_ramark_04);
        String s04 = context.getResources().getString(R.string.privacy_ramark_05);
        String s05 = context.getResources().getString(R.string.privacy_ramark_06);
        final SpannableString spannableTip1 = new SpannableString(s02);
        spannableTip1.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent();
                intent.setClass(context, WebviewActivity.class);
                intent.putExtra("url", ConstDefine.HttpAdress + "Uploads/privacyagreement/privacyagreement.html");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(context.getResources().getColor(R.color.colorAccent)); //设置颜色
            }
        }, 0, s02.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        final SpannableString spannableTip2 = new SpannableString(s04);
        spannableTip2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent();
                intent.setClass(context, WebviewActivity.class);
                intent.putExtra("url", ConstDefine.HttpAdress + "Uploads/privacyagreement/privacyagreement.html");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(context.getResources().getColor(R.color.colorAccent)); //设置颜色
            }
        }, 0, s04.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        remark.setText(s01);
        remark.append(spannableTip1);
        remark.append(s03);
        remark.append(spannableTip2);
        remark.append(s05);
        remark.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }

    public interface OnItemClickListener {
        void onRight();
    }
}
