package com.cmput301.cs.project.project.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.cmput301.cs.project.R;
import com.cmput301.cs.project.project.model.Claim;

public class EditClaimActivity extends Activity {
    public static final String KEY_CLAIM = "key_claim";

    public static Intent intentWithClaim(Context context, Claim claim) {
        return new Intent(context, EditClaimActivity.class).putExtra(KEY_CLAIM, claim);
    }

    private static final int REQ_CODE_PICK_START_DATE = 1;
    private static final int REQ_CODE_PICK_END_DATE = 2;

    private Button mStartDateButt;
    private Button mEndDateButt;

    private Claim.Builder mBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_claim_activity);

        initBuilder();

        mStartDateButt = (Button) findViewById(R.id.startDateButt);
        mEndDateButt = (Button) findViewById(R.id.endDateButt);

        mStartDateButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CalendarActivity.Builder builder = new CalendarActivity.Builder(EditClaimActivity.this);
                if (mBuilder.isStartTimeSet()) {
                    builder.selectedDate(mBuilder.getStartTime());
                }
                if (mBuilder.isEndTimeSet()) {
                    builder.maxDate(mBuilder.getEndTime());
                }
                startActivityForResult(builder.build(), REQ_CODE_PICK_START_DATE);
            }
        });

        mEndDateButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CalendarActivity.Builder builder = new CalendarActivity.Builder(EditClaimActivity.this);
                if (mBuilder.isEndTimeSet()) {
                    builder.selectedDate(mBuilder.getEndTime());
                }
                if (mBuilder.isStartTimeSet()) {
                    builder.minDate(mBuilder.getStartTime());
                }
                startActivityForResult(builder.build(), REQ_CODE_PICK_END_DATE);
            }
        });
    }

    private void initBuilder() {
        final Claim claim = getIntent().getParcelableExtra(KEY_CLAIM);
        mBuilder = claim == null ? new Claim.Builder() : Claim.Builder.copyFrom(claim);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        final long date = data.getLongExtra(CalendarActivity.KEY_DATE, -1);
        switch (requestCode) {
            case REQ_CODE_PICK_START_DATE:
                if (resultCode == RESULT_OK && date != -1) {
                    mBuilder.startTime(date);
                }
                break;
            case REQ_CODE_PICK_END_DATE:
                if (resultCode == RESULT_OK && date != -1) {
                    mBuilder.endTime(date);
                }
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_claim, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -363,16 +363,46 @@
 Claim;%0A%0A
+import java.text.DateFormat;%0A%0A
 public c
@@ -889,16 +889,52 @@
 Builder;
+%0A    private DateFormat mDateFormat;
 %0A%0A    @O
@@ -1093,24 +1093,105 @@
 activity);%0A%0A
+        mDateFormat = android.text.format.DateFormat.getMediumDateFormat(this);%0A%0A
         init
@@ -2683,19 +2683,12 @@
     
-mBuilder = 
+if (
 clai
@@ -2700,63 +2700,160 @@
 null
- ? new Claim.Builder() : Claim.Builder.copyFrom(claim);
+) %7B%0A            mBuilder = new Claim.Builder();%0A        %7D else %7B%0A            mBuilder = Claim.Builder.copyFrom(claim);%0A            updateUI();%0A        %7D
 %0A   
@@ -3203,32 +3203,64 @@
 tartTime(date);%0A
+                    updateUI();%0A
                 
@@ -3422,32 +3422,64 @@
 .endTime(date);%0A
+                    updateUI();%0A
                 
@@ -3574,39 +3574,227 @@
 ata);%0A        %7D%0A
-
     %7D%0A%0A
+    private void updateUI() %7B%0A        mStartDateButt.setText(mDateFormat.format(mBuilder.getStartTime()));%0A        mEndDateButt.setText(mDateFormat.format(mBuilder.getEndTime()));%0A    %7D%0A%0A%0A
     @Override%0A  

update ui for da edit claim