package ai.pai.lensman.briefs;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.umeng.analytics.MobclickAgent;

import ai.pai.lensman.R;
import ai.pai.lensman.activities.WebViewActivity;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.bean.BriefsBean;
import ai.pai.lensman.settings.SettingsActivity;
import ai.pai.lensman.utils.UmengEvent;
import ai.pai.lensman.utils.UrlContainer;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BriefsActivity extends BaseActivity implements BriefsContract.View{

    @BindView(R.id.tv_today_upload_num)     TextView todayUploadNumText;
    @BindView(R.id.tv_today_point_amount)   TextView todayPointAmountText;
    @BindView(R.id.tv_week_origin_sold_num) TextView weekOriginSoldNumText;
    @BindView(R.id.tv_week_point_amount)    TextView weekPointAmountText;
    @BindView(R.id.switcher_order_msg)      TextSwitcher orderMsgSwitch;
    @BindView(R.id.switcher_system_msg)     TextSwitcher systemMsgSwitch;
    @BindView(R.id.tv_box_num)              TextView boxNumText;
    @BindView(R.id.tv_box_status)           TextView boxStatusText;
    @BindView(R.id.tv_current_price)        TextView currentPriceText;

    private BriefsPresenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_briefs);
        unbinder = ButterKnife.bind(this);

        ViewSwitcher.ViewFactory factory = new ViewSwitcher.ViewFactory() {

            public View makeView() {
                TextView myText = new TextView(BriefsActivity.this);
                myText.setGravity(Gravity.CENTER_VERTICAL);
                myText.setTextSize(16);
                myText.setTextColor(Color.parseColor("#333333"));
                return myText;
            }
        };
        orderMsgSwitch.setFactory(factory);
        systemMsgSwitch.setFactory(factory);
        presenter = new BriefsPresenter(this,this);
    }

    @OnClick(R.id.switcher_order_msg)
    public void clickOrderMsg(){
        presenter.clickCurrentOrderMsg();
    }

    @OnClick(R.id.switcher_system_msg)
    public void clickSysMsg(){
        presenter.clickCurrentSysMsg();
    }

    @OnClick(R.id.title_bar_back_layout)
    void back(){
        finish();
    }

    @OnClick(R.id.iv_settings)
    void goToSettingPage(){
        MobclickAgent.onEvent(this, UmengEvent.brief_setting_btn_click);
        startActivity(new Intent(this, SettingsActivity.class));
    }

    @OnClick(R.id.layout_price_setting)
    void goToSetPrice(){
        presenter.clickSettingPrice();
    }

    @OnClick(R.id.layout_price_introduction)
    void goToPriceIntroduction(){
        Intent intent = new Intent(this,WebViewActivity.class);
        intent.putExtra("url", UrlContainer.PLATFORM_PRICE_RULES_PAGE_URL);
        intent.putExtra("title",getString(R.string.platform_price_rule));
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.stop();
    }

    @Override
    public void updateUserInfo(BriefsBean bean) {
        todayUploadNumText.setText(String.valueOf(bean.todayUpload));
        weekPointAmountText.setText(String.valueOf(bean.weekIncome));
        todayPointAmountText.setText(String.valueOf(bean.todayIncome));
        weekOriginSoldNumText.setText(String.valueOf(bean.weekSold));
    }

    @Override
    public void updateMessageBar(final String sysMsg, final String orderMsg) {
        systemMsgSwitch.post(new Runnable() {
            @Override
            public void run() {
                orderMsgSwitch.setText(TextUtils.isEmpty(orderMsg) ? getString(R.string.no_order_msg) : orderMsg);
                systemMsgSwitch.setText(TextUtils.isEmpty(sysMsg) ? getString(R.string.no_system_msg): sysMsg);
            }
        });

    }

    @Override
    public void updateBoxInfo(String boxNo, String boxStatus) {
        boxNumText.setText(boxNo);
        boxStatusText.setText(boxStatus);
    }

    @Override
    public void updatePriceInfo(String priceInfo) {
        currentPriceText.setText(priceInfo);
    }

}
