package com.danding.yang800;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.danding.yang800.Fragment.MarketPlatformFrag;
import com.danding.yang800.Fragment.MarketShopFrag;
import com.danding.yang800.Fragment.MyTicketNotUseFrag;
import com.danding.yang800.Fragment.MyTicketUsedFrag;
import com.danding.yang800.Presenter.MyTicketActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.ToastUtil;
import com.yanzhenjie.sofia.Sofia;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyTicketActivity extends BaseActivity {
    private FragmentManager fm = this.getFragmentManager();
    private FragmentTransaction ft;
    private MyTicketUsedFrag usedFrag;
    private MyTicketNotUseFrag notUseFrag;
    @BindView(R.id.myTicket_content)
    FrameLayout myTicket_content;
    @BindView(R.id.myTicket_usedLayout)
    RelativeLayout myTicket_usedLayout;
    @BindView(R.id.myTicket_notUseLayout)
    RelativeLayout myTicket_notUseLayout;
    @BindView(R.id.myTicket_backTv)
    TextView myTicket_backTv;
    @BindView(R.id.myTicket_usedTv)
    TextView myTicket_usedTv;
    @BindView(R.id.myTicket_usedIv)
    TextView myTicket_usedIv;
    @BindView(R.id.myTicket_notUseTv)
    TextView myTicket_notUseTv;
    @BindView(R.id.myTicket_notUseIv)
    TextView myTicket_notUseIv;
    MyTicketActivityPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_my_ticket);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        presenter = new MyTicketActivityPresenter(this);
        presenter.getInfo();
        initFrag();
    }

    @OnClick({R.id.myTicket_backTv, R.id.myTicket_usedLayout, R.id.myTicket_notUseLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.myTicket_backTv:
                finish();
                break;
            case R.id.myTicket_usedLayout:
                myTicket_usedTv.setTextColor(ContextCompat.getColor(this, R.color.pink));
                myTicket_usedIv.setVisibility(View.VISIBLE);
                myTicket_notUseTv.setTextColor(ContextCompat.getColor(this, R.color.gray));
                myTicket_notUseIv.setVisibility(View.INVISIBLE);
                ft = fm.beginTransaction();
                setSelected();
                showUsed();
                ft.commit();
                break;
            case R.id.myTicket_notUseLayout:
                myTicket_notUseTv.setTextColor(ContextCompat.getColor(this, R.color.pink));
                myTicket_notUseIv.setVisibility(View.VISIBLE);
                myTicket_usedTv.setTextColor(ContextCompat.getColor(this, R.color.gray));
                myTicket_usedIv.setVisibility(View.INVISIBLE);
                ft = fm.beginTransaction();
                setSelected();
                showNotUse();
                ft.commit();
                break;
        }
    }

    private void initFrag() {
        ft = fm.beginTransaction();
        showUsed();
        ft.commit();
    }

    private void setSelected() {
        if (usedFrag != null) {
            ft.hide(usedFrag);
        }
        if (notUseFrag != null) {
            ft.hide(notUseFrag);
        }

    }

    private void showUsed() {
        if (usedFrag == null) {
            usedFrag = new MyTicketUsedFrag(this);
            ft.add(R.id.myTicket_content, usedFrag);
        } else {
            ft.show(usedFrag);
        }
    }

    private void showNotUse() {
        if (notUseFrag == null) {
            notUseFrag = new MyTicketNotUseFrag(this);
            ft.add(R.id.myTicket_content, notUseFrag);
        } else {
            ft.show(notUseFrag);
        }
    }

    public void initData(int unusedCount, int expireCount) {
        myTicket_usedTv.setText("未使用（" + unusedCount + "）");
        myTicket_notUseTv.setText("已失效（" + expireCount + "）");
    }

    public void reFresh() {
        presenter.getInfo();
    }
}
