package com.taikor.investment.modules.find;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.taikor.investment.R;
import com.taikor.investment.base.BaseActivity;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.KeyBoardUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 研究报告
 * Created by Any on 2017/4/5.
 */
public class ReportActivity extends BaseActivity {

    @BindView(R.id.et_find)
    EditText etFind;
    @BindView(R.id.ib_clear)
    ImageButton ibClear;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_bar_title)
    TextView tvTitle;

    private boolean flag = false;//true默认页，false搜索页
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    public Fragment mContent, defaultReport, searchReport;

    @Override
    public int getContentViewId() {
        return R.layout.activity_find_report;
    }

    @Override
    public void initView() {
        tvTitle.setText(R.string.report);
        etFind.setHint(R.string.keyword);
        etFind.addTextChangedListener(watcher);
        etFind.setOnKeyListener(onKeyListener);

        defaultReport = new DefaultReportFragment();
        searchReport = new SearchReportFragment();
        setDefaultFragment(defaultReport);//默认显示的页面
    }

    //设置默认的显示页面
    public void setDefaultFragment(Fragment fragment) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_report, fragment).commit();
        mContent = fragment;
    }

    // 切换fragment的显示隐藏
    public void switchContent(Fragment showFragment) {
        if (mContent != showFragment) {
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            if (!showFragment.isAdded()) {// 先判断是否被add过    
                fragmentTransaction.hide(mContent).add(R.id.fl_report, showFragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                fragmentTransaction.hide(mContent).show(showFragment).commit(); // 隐藏当前的fragment，显示下一个
            }
            mContent = showFragment;
        }
    }

    @OnClick({R.id.tv_bar_back, R.id.ib_clear, R.id.tv_cancel})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.tv_bar_back:
                finish();
                break;
            case R.id.ib_clear:
                etFind.setText("");
                ibClear.setVisibility(View.GONE);
                if (flag) {
                    tvCancel.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_cancel:
                //切换回默认显示的研究报告
                switchContent(defaultReport);
                if (!flag) {
                    tvCancel.setVisibility(View.GONE);
                }
                flag = true;
                break;
        }
    }

    //替换成搜索列表页
    private View.OnKeyListener onKeyListener = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                KeyBoardUtils.hideSoftKeyboard(v);
                switchContent(searchReport);
                flag = false;

                return true;
            }
            return false;
        }
    };

  //监听文本的变化
    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //文本输入之后，显示清除图标和取消按钮
            ibClear.setVisibility(View.VISIBLE);
            tvCancel.setVisibility(View.VISIBLE);
        }
    };

    //埋点
    @Override
    protected void onResume() {
        super.onResume();
        TAnalytics.onPageStart("reportListPage");
    }

    @Override
    protected void onPause() {
        super.onPause();
        TAnalytics.onPageLoad("reportListPage","homePage","","");
    }
}
