package com.uiuipad.appstore.activity;

import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.uiuipad.appstore.R;
import com.uiuipad.appstore.adapter.AppAdapter;
import com.uiuipad.appstore.base.BaseActivity;
import com.uiuipad.appstore.bean.AppInfo;
import com.uiuipad.appstore.base.BaseResponse;
import com.uiuipad.appstore.common.CommonConfig;
import com.uiuipad.appstore.network.NetInterfaceManager;
import com.uiuipad.appstore.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


public class SearcherActivity extends BaseActivity {
    private static final String TAG = SearcherActivity.class.getSimpleName();

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.edit_search)
    EditText edit_search;
    @BindView(R.id.tv_search)
    TextView tv_search;

    private AppAdapter mAppAdapter;

    /**
     * 设置布局
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_searcher;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

        edit_search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        edit_search.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        edit_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            //do it
                            if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                                //do something;
                                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                                        .hideSoftInputFromWindow(SearcherActivity.this.getCurrentFocus()
                                                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                                String text = edit_search.getText().toString();
                                checkContent(text);
                            }
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }
        });
        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = edit_search.getText().toString();
                checkContent(text);
            }
        });

    }

    @Override
    public void initData() {
        mAppAdapter = new AppAdapter(SearcherActivity.this);
        mAppAdapter.setHasStableIds(true);
        mAppAdapter.setLineShow(false);
        recyclerView.setAdapter(mAppAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void checkContent(String text) {
        if (TextUtils.isEmpty(text)) {
            getAllAppPackageName();
        } else {
            int setting_admin_app = Settings.System.getInt(getContentResolver(), CommonConfig.APP_ADMIN, 1);
            if (setting_admin_app == 0) {
                ToastUtil.debugShow("搜索功能已禁用");
            } else {
                searcheApp(text);
            }
        }
    }

    private void searcheApp(String text) {

    }

    private void getAllAppPackageName() {

    }

    public void finish(View view) {
        finish();
    }

}
