package com.taikor.investment.modules.customer;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.taikor.investment.R;
import com.taikor.investment.base.BaseActivity;
import com.taikor.investment.bean.Products;
import com.taikor.investment.event.DataEvent;
import com.taikor.investment.utils.DegreeUtils;
import com.taikor.investment.utils.KeyBoardUtils;
import com.taikor.investment.utils.TimeUtils;
import com.taikor.investment.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 基金详情,保存后跳转到持仓设置
 * Created by Any on 2017/4/12.
 */
public class FundDescActivity extends BaseActivity implements TextView.OnEditorActionListener {

    @BindView(R.id.tv_fund_desc_name)
    TextView tvFundName;//基金名称
    @BindView(R.id.tv_fund_desc_id)
    TextView tvFundId;//基金编码
    @BindView(R.id.tv_score)
    TextView tvScore;//分数
    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;//标题
    @BindView(R.id.et_money)
    EditText etMoney;//投资金额
    @BindView(R.id.tv_date)
    TextView tvDate;//日期
    @BindView(R.id.rb_time_front)
    RadioButton rbTimeFront;//3点前
    @BindView(R.id.rb_time_behind)
    RadioButton rbTimeBehind;//3点后
    @BindView(R.id.rb_front)
    RadioButton rbFront;//前端
    @BindView(R.id.rb_behind)
    RadioButton rbBehind;//后端
    @BindView(R.id.rb_fu_hao)
    RadioButton rbFuHao;//%
    @BindView(R.id.rb_yuan)
    RadioButton rbYuan;//元
    @BindView(R.id.trans)
    RadioGroup trans;
    @BindView(R.id.et_proportion)
    EditText etProportion;//交易费率
    @BindView(R.id.rb_once)
    RadioButton rbOnce;//红利再投
    @BindView(R.id.rb_cache)
    RadioButton rbCache;//现金分红
    @BindView(R.id.tv_net_fit)
    TextView tvNetFit;//净值
    @BindView(R.id.tv_trade_num)
    TextView tvTradeNum;//份额
    @BindView(R.id.tv_ok)
    TextView tvOk;//确定

    private double netWorth, purchaseAmount, tradingShare;
    private String productId, productName, transactionRate = "%";
    private int purchasePoint = 1, chargeType = 1, dividend = 1;
    private ArrayList<Products> repo;//传递给持仓设置页面

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

    @Override
    public void initView() {
        tvBarTitle.setText("基金购买");
        tvScore.setVisibility(View.GONE);
        rbTimeFront.setChecked(true);
        rbFront.setChecked(true);
        rbFuHao.setChecked(true);
        rbOnce.setChecked(true);
        tvDate.setText(getTime(new Date()));
        etMoney.setOnEditorActionListener(this);

        //获取从基金列表获取过来的数据
        Intent intent = getIntent();
        productId = intent.getStringExtra("product_id");
        productName = intent.getStringExtra("product_name");
        netWorth = intent.getDoubleExtra("product_net_worth", 0);
        repo = (ArrayList<Products>) intent.getSerializableExtra("repo");

        tvFundId.setText(productId);
        tvFundName.setText(productName);
        tvNetFit.setText(DegreeUtils.setDoubleFour(netWorth));
    }

    @OnClick({R.id.tv_bar_back, R.id.tv_date, R.id.rb_time_front, R.id.rb_time_behind, R.id.rb_front,
            R.id.rb_behind, R.id.rb_fu_hao, R.id.rb_yuan, R.id.rb_once, R.id.rb_cache, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bar_back:
                finish();//返回到基金列表
                break;
            case R.id.tv_date:
                KeyBoardUtils.hideSoftKeyboard(tvDate);
                setDate();
                break;
            case R.id.rb_time_front:
                purchasePoint = 1;
                break;
            case R.id.rb_time_behind:
                purchasePoint = 2;
                break;
            case R.id.rb_front:
                chargeType = 1;
                break;
            case R.id.rb_behind:
                chargeType = 2;
                break;
            case R.id.rb_fu_hao:
                transactionRate = "%";
                break;
            case R.id.rb_yuan:
                transactionRate = "元";
                break;
            case R.id.rb_once:
                dividend = 1;
                break;
            case R.id.rb_cache:
                dividend = 2;
                break;
            case R.id.tv_ok:
                transmitData();
                break;
        }
    }

    long purchaseDate = 0;
    
    //传递数据
    private void transmitData() {
//        String date = tvDate.getText().toString();
//        if (!TextUtils.isEmpty(date)) {
//            purchaseDate = TimeUtils.stringToLong(date);
//        }
        //跳转到持仓设置,新增一条数据
        Products products = new Products();
        products.setProductID(productId);
        products.setProductName(productName);
        products.setPurchaseAmount(purchaseAmount);
        products.setPurchaseDate(purchaseDate);
        products.setPurchasePoint(purchasePoint);
        products.setChargeType(chargeType);
        products.setTransactionRate(transactionRate);
        products.setDividend(dividend);
        products.setNetWorth(netWorth);
        products.setTradingShare(tradingShare);
        repo.add(repo.size(), products);

        EventBus.getDefault().post(new DataEvent(repo));
        finish();
    }

    //设置时间格式
    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 设置时间
     */
    private void setDate() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2016, 1, 1);

        Calendar endDate = Calendar.getInstance();
        endDate.set(2020, 1, 1);
        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                purchaseDate=date.getTime();
                tvDate.setText(getTime(date));
            }
        }).setType(TimePickerView.Type.YEAR_MONTH_DAY)
                .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setDividerColor(Color.DKGRAY)
                .setContentSize(16)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    //投资金额
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            KeyBoardUtils.hideSoftKeyboard(v);
            String investMoney = etMoney.getText().toString();
            if (TextUtils.isEmpty(investMoney))
                ToastUtils.showShort(FundDescActivity.this, "您还没有输入金额！");
            else {
                purchaseAmount = Double.valueOf(investMoney);
                tradingShare = purchaseAmount / netWorth;
                tvTradeNum.setText(DegreeUtils.setDoubleTwo(tradingShare));
            }
        }
        return true;
    }
}
