package com.yyrh.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yyrh.ui.dialog.CustomExitGameDialog;

import com.yyrh.ui.userlist.DatabaseHelper;
import com.yyrh.utils.ResourceUtil;


public class AccountLoginListView extends RelativeLayout {

    public ImageView iv_account_icon;
    public TextView tv_account_type,tv_account;
    public Button btn_common_use,btn_delete;
    public String password,login_token;
    public AccountLoginListView(Context context) {
        super(context);
        intView(context);
    }

    public AccountLoginListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        intView(context);
    }

    public AccountLoginListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intView(context);
    }

    void intView(final Context context){

        LayoutInflater.from(context).inflate(ResourceUtil.getLayoutId(context,"hnyy_login_list"), this);
        iv_account_icon = findViewById(ResourceUtil.getId(context,
                "iv_account_icon"));
        tv_account_type = findViewById(ResourceUtil.getId(context,
                "tv_account_type"));
        tv_account = findViewById(ResourceUtil.getId(context,
                "tv_account"));
        btn_common_use = findViewById(ResourceUtil.getId(context,
                "btn_common_use"));
        btn_delete = findViewById(ResourceUtil.getId(context,
                "btn_delete"));
        btn_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                CustomExitGameDialog.Builder builder = new CustomExitGameDialog.Builder(context);
                builder.Create().show();
                builder.setPositiveButton(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        deleteItem(tv_account.getText().toString(),context);
                        removeAllViews();


                        dialog.dismiss();
                    }
                });
                builder.setnegativeButton(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                });

                builder.setTitle( context.getString(ResourceUtil.getStringId(context, "hnyy_delete_account_tip")) + tv_account.getText().toString());

            }
        });

    }

    private void deleteItem(String uname,Context context){


        DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = null;
        db = dbHelper.getReadableDatabase();
        String sql = "delete from user where name = '"+uname+"'";
        //执行SQL语句
        db.execSQL(sql);
        db.close();

    }

}
