package com.hjx.miaohongentry.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.hjx.miaohongentry.R;
import com.hjx.miaohongentry.adapter.BookListAdapter;
import com.hjx.miaohongentry.bean.BookInfo;
import com.hjx.miaohongentry.db.MHDataManager;
import com.hjx.miaohongentry.util.Constants;

import java.util.ArrayList;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class ChooseBookActivity extends Activity {


    private ListView listView;
    private BookListAdapter adapter;
    private ArrayList<BookInfo> bookList;
    private int curIndex;
    private Button okBtn;
    public static final int RESULT_CODE_CHOOSE_SECCESS = 0x01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_choose_book);
        curIndex = getIntent().getIntExtra("idx",0);
        okBtn = (Button)findViewById(R.id.btn_ok);
        listView = (ListView) findViewById(R.id.lv_book_list);
        bookList = MHDataManager.getList(Constants.MH_DATABASE_PATH);
        adapter = new BookListAdapter(this,bookList);
        adapter.setSelectItem(curIndex);
        listView.setAdapter(adapter);
        listView.setSelection(curIndex);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                curIndex = i;
                adapter.setSelectItem(curIndex);
            }
        });
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BookInfo bookInfo = bookList.get(curIndex);
                Intent mIntent = new Intent();
                mIntent.putExtra("id",bookInfo.get_id());
                mIntent.putExtra("press", bookInfo.getPress());
                mIntent.putExtra("reactos", bookInfo.getReactOS());
                mIntent.putExtra("grade", bookInfo.getGrade());
                // 设置结果，并进行传送
                setResult(RESULT_CODE_CHOOSE_SECCESS, mIntent);
                finish();
            }
        });
    }




}
