package com.genlot.ninglottery.widget;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseActivity;
import com.genlot.ninglottery.game.GameManager;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.module.function.FunctionActivity;
import com.genlot.ninglottery.module.hardwaretest.HardwareTestActivity;
import com.genlot.ninglottery.module.info.InfoActivity;
import com.genlot.ninglottery.module.login.LoginActivity;
import com.genlot.ninglottery.module.report.ReportActivity;
import com.genlot.ninglottery.widget.Diaolog.CommonDialog;
import com.genlot.ninglottery.widget.Diaolog.ViewConvertListener;

import java.io.IOException;

/**
 * @author ZhuYongKang
 * @description BottomView
 * @date 2019/8/22
 */
public class BottomView extends FrameLayout implements View.OnClickListener{

    private Button btFunction;
    private TextView tvNotice;
    private Context mContext;

    public BottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_bottom, this, true);
        btFunction = view.findViewById(R.id.bt_function);
        tvNotice = view.findViewById(R.id.tv_notice);
        btFunction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        initFunctionMenu();
    }

    private void initFunctionMenu() {
        MenuPopWindow menuPopWindow = MenuPopWindow.getInstance(mContext);
        menuPopWindow.show(btFunction);
        menuPopWindow.setBtnClick(id -> {
            switch (id) {
                case R.id.rb_rpt:
                    mContext.startActivity(new Intent(mContext, ReportActivity.class));
                    break;
                case R.id.rb_function:
                    mContext.startActivity(new Intent(mContext, FunctionActivity.class));
                    break;
                case R.id.rb_info:
                    mContext.startActivity(new Intent(mContext, InfoActivity.class));
                    break;
                case R.id.rb_hardware_check:
                    mContext.startActivity(new Intent(mContext, HardwareTestActivity.class));
                    break;
                case R.id.rb_lock:
                    showLockDialog();
                    break;
                case R.id.rb_logout:
                    mContext.startActivity(new Intent(mContext, LoginActivity.class));
                    break;
                case R.id.rb_close:
                    showShutDownDialog();
                    break;
            }
            menuPopWindow.dismiss();
        });
    }

    private void showShutDownDialog() {
        CommonDialog.newInstance().setLayoutId(R.layout.dialog_shut_down)
                .setConvertListener((ViewConvertListener) (holder, dialog) -> {
                    holder.setOnClickListener(R.id.bt_left, v -> dialog.dismiss());
                    holder.setOnClickListener(R.id.bt_middle, v -> {
                        try {
                            Runtime.getRuntime().exec("reboot -p");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                    holder.setOnClickListener(R.id.bt_right,  v -> {
                        try {
                            Runtime.getRuntime().exec("reboot");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                })
                .setSize(430, 270)
                .setOutCancel(false)
                .show(((BaseActivity)mContext).getSupportFragmentManager());
    }

    private void showLockDialog() {
        CommonDialog.newInstance().setLayoutId(R.layout.dialog_lock_screen)
                .setConvertListener((ViewConvertListener) (holder, dialog) -> {
                    KeyboardEditText etPassword = holder.getView(R.id.et_admin_password);
                    etPassword.setOnTextFinishListener(value -> {
                        if (GameManager.getPassword().equals(value)) {
                            dialog.dismiss();
                        } else if (!TextUtils.isEmpty(value)) {
                            etPassword.setError("输入密码错误");
                        }
                    });
                })
                .setSize(ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight())
                .setOutCancel(false)
                .setDimAmout(0.8f)
                .show(((BaseActivity)mContext).getSupportFragmentManager());
    }

    /**
     * 显示提示信息
     * @param message String
     */
    public void showTips(String message) {
        tvNotice.setText(message);
    }

}
